帖子未在laravel中显示

时间:2019-01-20 18:59:53

标签: post laravel-5.6

我的“帖子”表中有四个帖子。但它显示“无帖子”。

在控制器中:

use App\post;
public function index()
{
    $asd = post::all();
    return view ('posts.index')->with('post',$asd);
}

在index.blade.php中:

@extends('layout.app')

@section('content')
       <h1> Peoples</h1>
       <hr>
@if(count($post)>1)
       @foreach($post as $single_post)
          <p> {{$single_post->username}}  </p>
       @endforeach
@else
       <p> No Posts  </p>
@endif
@endsection

我不知道为什么会这样,请提供帮助!

1 个答案:

答案 0 :(得分:0)

在控制器中:

use App\post;
public function index()
{
    $posts = post::get();
    return view ('posts.index',compact('posts'));
}

在帖子/index.blade.php:


@extends('layout.app')

@section('content')
       <h1> Peoples</h1>
       <hr>
@if(count($post)>1)
       @foreach($posts as $post)
          <p> {{$post->username}}  </p>
       @endforeach
@else
       <p> No Posts  </p>
@endif
@endsection