dtpxi88884 2017-01-17 11:24
浏览 38

Laravel试图输出帖子,但控制器功能似乎工作错误

I'm trying to output my posts and for that in a specific page I call a function with foreach to load all the posts, and the function in my controller is something like:

public function getDashboard()
{
    $posts = Post::all();
    return view('dashboard', ['posts' => $posts]);
}

and my route is something like this:

Route::get('/dashboard',[
    'uses' => 'PostController@getDashboard',
    'as' => 'dashboard'

]);

but get an error when I try to load the page when i'm not logged in the error is something like: Undefined variable: posts (View: C:\wamp64\www\projectnameesources\views\dashboard.blade.php)

Anything you say is highly appreciated.

(I use Laravel 5.3.26 )

Here you go this is my blade, but I don't think this is where the problem is:

@extends('layouts.master')
 @section('content')
  @foreach($posts as $post)
    <article class="post">
        <div class="media w3-border-left w3-border-blue">
            <div class="media-left media-top">

            </div>
            <div class="media-body">
                <header><h4 class="media-heading">{{ $post->title }}</h4></header>
                <p>{{ $post->body }}</p>
                <div class="info">
                    Posted by {{ $post->user->name }} on {{ $post->created_at }}
                </div>
                <div class="interaction">
                    <a href="#" class="like">Like</a> |
                    <a href="#" class="like">Dislike</a> |
                    <a href="#" class="like">Edit</a> |
                    <a href="#" class="like">Delete</a>
                </div>

            </div>
        </div>
        <br>
    </article>
  @endforeach
 @endsection
  • 写回答

2条回答 默认 最新

  • dsfds656545 2017-01-17 12:48
    关注

    Try:

    public function getDashboard()
    {
        $posts = Post::all();
        return view('dashboard', compact('posts'));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探