此集合实例laravel框架上不存在属性[title]



我正在向show.blade.php显示数据库中的数据,但这个问题正在显示以下是代码:区块控制器

public function show(Blog $id)
{
//
$blogs = Blog::findOrfail($id);
return view('blog.show', compact('blogs'));
}

show.blade.php

@section('content')

<h3>{{ $blogs->title }}</h3>

@endsection

dd的输出($blogs(:形象我尝试了所有的选择,但仍然没有成功。。

具有该方法签名的$id是Model实例。Eloquent模型实现了IlluminateContractsSupportArrayable。如果将Arrayable或数组传递给findOrFail正在调用的find,它将像查找多条记录一样处理此问题,并返回一个Collection。

您有一个集合,而不是模型实例。

如果你的方法签名没有键入hint the Model,那么它会更有意义,因为你会有"id"。如果您为这些方法键入提示模型,通常您将使用路由模型绑定。

路由模型绑定:

public function show(Blog $blog)
{
// $blog is the matching record
}

没有模型绑定,只获取参数:

public function show($blog)
{
// $blog is just the 'id' or what ever you decided to pass in the URL
}

您的路由有一个名为blog的参数,因为您的资源名为"博客"。要使隐式路由模型绑定就位,必须将参数名称与签名中的参数名称相匹配。

在函数show(Blog $id)中,$id是Blog::class 的实例

你下一步需要做什么$blogs = Blog::findOrfail($id->id);

最新更新