【发布时间】:2018-05-29 20:49:47
【问题描述】:
我在 laravel 5.6 中使用了很多属性,当我 dd $phonebooks 时,我看到所有关系都正常工作,一切都很好,但是当我尝试在视图中显示它们时,我得到属性错误不存在在这个集合上 这是关系代码
public function client() {
return $this->hasMany('App\Client','id' , 'client_id');
}
这里是控制器
public function index()
{ $phonebooks = Phonebook::with('client')->get();
return view('admin.phonebooks.index',compact('phonebooks',$phonebooks));
}
最后是我如何尝试在视图中显示它们
<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>
这里是 dd 的结果只是它的一部分
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
就这样下去了。
【问题讨论】:
-
$phonebook->Client->title,c不应该小写吗?
-
尝试客户端而不是客户端
-
我的代码是小写的对不起我在这里输入的错误,但都试过了,没有运气
-
尝试用
compact('phonebooks')替换compact('phonebooks', $phonebooks) -
@PhilCross 刚刚做到了,但没有运气,同样的错误:(
标签: php laravel view controller