亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未定義變量:posts

未定義變量:posts

PHP
墨色風雨 2021-06-30 18:02:12
我是 laravel 的新手,我只是創建一個簡單的應用程序,用戶可以在其中登錄和注冊,然后在登錄后在儀表板中寫帖子,我試圖在儀表板中顯示用戶帖子數據,該數據保存在數據庫中,但是我收到此錯誤:未定義變量:posts(視圖:C:\xampp\htdocs\practiseapp\resources\views\dashboard.blade.php)我想我已經定義了帖子,我不知道出了什么問題,任何幫助將不勝感激謝謝....我的儀表板:@extends('layout.master')@section('content')<div class="col-sm-6 col-sm-offset-3">    <header><h3>What do you wanna say</h3></header>    <form method="post" action="{{route('post.create')}}">        {{csrf_field()}}        <div class="panel-body">        <div class="form-group">            <textarea class="form-control" name="body" id="body" rows="5" placeholder="Enter your post">            </textarea>        </div>        <button type="submit" class="btn btn-primary">Create post</button>    </form>    </div></div><section class="row-posts">    <div class="col-md-6 col-sm-offset-3">        <header><h3>Other people posts</h3></header>        @foreach($posts as $post)        <article class="post">            <p>{{ $post->body }}</p>            <div class="info">                posted by {{ $post->user->name}} on {{$post->created_at}}            </div>            <div class="interaction">                <a href="#">Like</a>|                <a href="#">Disike</a>|                <a href="#">Edit</a>|                <a href="#">Delete</a>              </div>        </article>        @endforeach    </div></section>@endsection
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

我認為你很接近,但我有一些想法可能對你有幫助。


首先,您是否檢查過您的路線設置是否正確routes/web.php?如果您使用了 Laravel 文檔中的一些示例,則您的路由可能會在不使用您編寫的控制器的情況下返回視圖。如果你有這樣的事情:


Route::get('/', function () {

    return view('dashboard');

});

...那么你可能想用這樣的東西替換它:


Route::get( '/', 'PostController@show );

管理路由的方法有很多種——Laravel Docs會很好地解釋其中的一些。


此外,當將東西從控制器傳遞到視圖時,我喜歡將我的對象分配給關聯數組,然后在使用視圖方法時傳遞該數組。這完全是個人喜好,但您可能會發現它很有用。有點像這樣:


public function show()

{

    // Create output array - store things in here...

    $output = [];


    $output[ "posts" ] = Post::all();


    // Render the Dashboard view with data...

    return view( 'dashboard', $output );

}

希望有些幫助!


查看完整回答
反對 回復 2021-07-02
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

試試下面的代碼,


<?php


    namespace App\Http\Controllers;


    use App\Http\Requests;

    use App\Post;

    use App\UserTypes;


    use Auth;

    use Hashids;

    use Redirect;

    use Illuminate\Http\Request;

    use Hash;


    class PostController extends Controller

    {


        public function show()

        {

            //Fetching all the posts from the database

            $posts = Post::get();

            return view('dashboard', compact('posts'));

        }


        public function store(Request $request)

        {

            $this->validate($request,[

                'body' => 'required'


            ]);


            $post = new Post;

            $post->body = $request->body;

            $request->user()->posts()->save($post);


            return redirect()->route('dashboard');      

        }

    }


查看完整回答
反對 回復 2021-07-02
  • 2 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號