我有表格Authors
和表格Uploads
,我只是想通过author_id
表格中的Uploads
获取特定作者的所有上传内容。
为此目的,我的控制器中有一个方法如下:
public function author_works()
{
$uploads= Upload::where('author_id', 3)->first();
return view('author-works')->with('uploads',$uploads);
}
我的观点是:
@extends('layouts.app')
@section('content')
<h1>Uploads</h1>
@if(count($uploads)>0)
@foreach($uploads as $upload)
<div class="well">
<h3><a href="/uploads/{{$upload->id}}">{{$upload->name}}</a> </h3>
<small>Written on {{$upload->created_at}}</small>
</div>
@endforeach
{{-- {{$uploads->links()}}--}}
@else
<p>No uploads found</p>
@endif
@endsection
当我在控制器中使用dd($uploads);
进行调试时,我看到以下内容,这实际上是正确的记录:
#attributes: array:9 [▼
"id" => 7
"name" => "Fsdf"
"description" => "fsdfsdffds"
"created_at" => "2017-12-07 21:29:53"
"updated_at" => "2017-12-07 21:29:53"
"user_id" => 1
"avtor_id" => 3
]
#original: array:9 [▼
"id" => 7
"name" => "Fsdf"
"description" => "fsdfsdffds"
"created_at" => "2017-12-07 21:29:53"
"updated_at" => "2017-12-07 21:29:53"
"user_id" => 1
"avtor_id" => 3
]
有人可以解释我为什么会收到错误吗?
答案 0 :(得分:2)
当您使用return true
方法you're getting an object instead of collection时。使用first()
:
get()