猿问

未定义变量:posts

我是 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


墨色风雨
浏览 114回答 2
2回答

达令说

我认为你很接近,但我有一些想法可能对你有帮助。首先,您是否检查过您的路线设置是否正确routes/web.php?如果您使用了 Laravel 文档中的一些示例,则您的路由可能会在不使用您编写的控制器的情况下返回视图。如果你有这样的事情:Route::get('/', function () {&nbsp; &nbsp; return view('dashboard');});...那么你可能想用这样的东西替换它:Route::get( '/', 'PostController@show );管理路由的方法有很多种——Laravel Docs会很好地解释其中的一些。此外,当将东西从控制器传递到视图时,我喜欢将我的对象分配给关联数组,然后在使用视图方法时传递该数组。这完全是个人喜好,但您可能会发现它很有用。有点像这样:public function show(){&nbsp; &nbsp; // Create output array - store things in here...&nbsp; &nbsp; $output = [];&nbsp; &nbsp; $output[ "posts" ] = Post::all();&nbsp; &nbsp; // Render the Dashboard view with data...&nbsp; &nbsp; return view( 'dashboard', $output );}希望有些帮助!

神不在的星期二

试试下面的代码,<?php&nbsp; &nbsp; namespace App\Http\Controllers;&nbsp; &nbsp; use App\Http\Requests;&nbsp; &nbsp; use App\Post;&nbsp; &nbsp; use App\UserTypes;&nbsp; &nbsp; use Auth;&nbsp; &nbsp; use Hashids;&nbsp; &nbsp; use Redirect;&nbsp; &nbsp; use Illuminate\Http\Request;&nbsp; &nbsp; use Hash;&nbsp; &nbsp; class PostController extends Controller&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public function show()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Fetching all the posts from the database&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $posts = Post::get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return view('dashboard', compact('posts'));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public function store(Request $request)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->validate($request,[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'body' => 'required'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post = new Post;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post->body = $request->body;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $request->user()->posts()->save($post);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('dashboard');&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答