我的错误: undefined variable :asd(查看:C:\xampp\htdocs\kproject\resources\views\posts\index.blade.php)
Controller :
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($asd)>0)
@foreach($asd as $post)
<p> {{$post->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
我不知道为什么会出现此错误,请提供帮助!
最佳答案
您正在传递 $asd
变量来查看...但名称不同。 View 中的名称将为 $post
。
->with('post', $asd)
...传递 $asd 变量来查看并将其分配给 $post
将index.blade.php更改为:
@extends('layout.app')
@section('content')
<h1> Peoples</h1>
<hr>
@if(count($post)>0)
@foreach($post as $p)
<p> {{$p->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
关于laravel - laravel 帖子的 index.php 中 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056536/