此集合实例 Laravel 关系上不存在属性 [X] [英] Property [X] does not exist on this collection instance laravel relationship
问题描述
我在 laravel 5.6 中使用了很多属性,当我添加 $phonebooks 时,我看到所有关系都正常工作,一切都很好,但是当我尝试在视图中显示它们时,我得到属性的错误不存在在这个集合上这是关系代码
公共函数client() {返回 $this->hasMany('App\Client','id', 'client_id');}
这里是控制器
公共函数 index(){ $phonebooks = Phonebook::with('client')->get();return view('admin.phonebooks.index',compact('phonebooks',$phonebooks));}
最后这是我如何尝试在视图中显示它们
@foreach($phonebooks as $phonebook)<tr><th scope="row">{{$phonebook->id}}</th><th scope="row">{{$phonebook->title}}</th><td><a href="/admin/phonebooks/{{$phonebook->id}}">{{$phonebook->description}}</a></td><td>{{$phonebook->calldate}}</td><td>{{$phonebook->created_at->toFormattedDateString()}}</td><td>{{ $phonebook->client->title}}</td><td><div class="btn-group" role="group" aria-label="基本示例"><a href="{{ URL::to('admin/phonebooks/' . $phonebook->id .'/edit') }}"><button type="button" class="btn btn-warning">ویراییش</button></a> <form action="{{url('admin/phonebooks', [$phonebook->id])}}" method="POST"><input type="hidden" name="_method" value="DELETE"><input type="hidden" name="_token" value="{{ csrf_token() }}"><input type="submit" class="btn btn-danger" value="حذف"/></表单></td></tr>@endforeach</tbody>
这里是 dd 的结果只是其中的一部分
集合 {#630 ▼ #items: array:3 [▼0 =>电话簿{#572 ▼#fillable: 数组:5 [▶]#connection: "mysql"#table: 空#primaryKey: "id"#keyType: "int"+递增:真#和: []#withCount: []#每页:15+存在:真实+wasRecentlyCreated: 假#attributes: 数组:8 [▶]#original: 数组:8 [▶]#变化: []#casts: []#日期: []#dateFormat:空#appends: []#dispatchesEvents: []#observables: []#关系:数组:1 [▼客户" =>收藏{#627 ▼#items: 数组:1 [▼0 =>客户{#621 ▼#connection: "mysql"#table: 空#primaryKey: "id"#keyType: "int"+递增:真#和: []#withCount: []#每页:15+存在:真实+wasRecentlyCreated: 假#attributes:数组:16 [▼身份证"=>1
然后就这样下去了.
解决方案 问题是这一行:
{{ $phonebook->client->title}}
在你看来.
您已将关系设置为 hasMany
关系,这将返回模型集合.
如果您执行 dd($phonebook->client)
,它将返回一个集合,而不是单个模型.
它试图调用集合对象上的属性 title
,而不是模型.
您需要将关系定义更改为 hasOne()
,或者执行以下操作:
{{ $phonebook->client->first()->title }}
(或替代):
{{ $phonebook->client->get(0)->title }}
i am using has many realtions in laravel 5.6 and when i dd the $phonebooks i see all the relations are working properly and every thing is fine but when i try to show them in view i get the error of property does not exist on this collection
here is the relation code
public function client() {
return $this->hasMany('App\Client','id' , 'client_id');
}
and here is controller
public function index()
{ $phonebooks = Phonebook::with('client')->get();
return view('admin.phonebooks.index',compact('phonebooks',$phonebooks));
}
and finally here is how i try to show them in view
<tbody>
@foreach($phonebooks as $phonebook)
<tr>
<th scope="row">{{$phonebook->id}}</th>
<th scope="row">{{$phonebook->title}}</th>
<td><a href="/admin/phonebooks/{{$phonebook->id}}">{{$phonebook->description}}</a></td>
<td>{{$phonebook->calldate}}</td>
<td>{{$phonebook->created_at->toFormattedDateString()}}</td>
<td>{{ $phonebook->client->title}}</td>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<a href="{{ URL::to('admin/phonebooks/' . $phonebook->id . '/edit') }}">
<button type="button" class="btn btn-warning">ویراییش</button>
</a>
<form action="{{url('admin/phonebooks', [$phonebook->id])}}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-danger" value="حذف"/>
</form>
</div>
</td>
</tr>
@endforeach
</tbody>
and here is the result of dd just a part of it
Collection {#630 ▼ #items: array:3 [▼
0 => Phonebook {#572 ▼
#fillable: array:5 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"client" => Collection {#627 ▼
#items: array:1 [▼
0 => Client {#621 ▼
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [▼
"id" => 1
and is goes down just like this .
解决方案 The problem is this line:
{{ $phonebook->client->title}}
In your view.
You've setup your relationship as a hasMany
relationship, which will return a collection of models.
If you do dd($phonebook->client)
, it'll return a collection, not a single model.
It's trying to call the property title
on a collection object, not a model.
You need to change the relationship definiation to a hasOne()
, OR do something like:
{{ $phonebook->client->first()->title }}
(or alternatively):
{{ $phonebook->client->get(0)->title }}
这篇关于此集合实例 Laravel 关系上不存在属性 [X]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆