1
$attendees = Attendee::with('User')->get();
return View::make('admin.attendees.index', compact('attendees'));
與會者模式
public function user() | if(!($user->hasRole('admin') || $user->hasRole('programmer')))
{ | return Redirect::to('/');
return $this->belongsTo('User'); |
}
查看
@foreach($attendees as $attendee)
<td>{{link_to_route('admin.users.show', $attendee->user->username, $attendee->user->id)}}</td>
@endforeach
223查詢
select * from `users` where `users`.`id` = '4' limit 1600μs
select `roles`.*, `assigned_roles`.`user_id` as `pivot_user_id`, `assigned_roles`.`role_id` as `pivot_role_id` from `roles` inner join `assigned_roles` on `roles`.`id` = `assigned_roles`.`role_id` where `assigned_roles`.`user_id` = '4'630μs
select * from `attendees`1.24ms
select * from `users` where `users`.`id` in ('5', '1', '3', '8', '9', '10')780μs
select * from `users` where `users`.`id` = '5' limit 1680μs
select * from `users` where `users`.`id` = '5' limit 1650μs
select * from `users` where `users`.`id` = '5' limit 1680μs
select * from `users` where `users`.`id` = '5' limit 1590μs
select * from `users` where `users`.`id` = '1' limit 1
<continues like so for each user id>
我正在使用phpdebugbar來顯示查詢。
遷移
Schema::table('attendees', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('no action');
我是不是做錯了什麼是導致要在運行查詢一遍又一遍?
您可以顯示與會者模型已定義的關係嗎? – Chris
從查詢我可以理解你正在使用'Zizaco/entrust'軟件包。 我認爲這可能是你的問題。在你看來,你返回一個集合。您不查詢集合。一個集合已經加載,並通過刀片呈現。如果你使用的是演示者,那麼情況就不一樣了。 –
@Chris更新以顯示我與參加者的關係。 @ akad0是的,我正在使用'Zizaco/confide/entrust'。糾正我,如果我錯了,在我的控制器中,Laravel會向與會者和用戶詢問是否將集合返回給具有與會者和相關用戶的視圖。在我看來,我只想訪問集合,但它似乎正在做更多的查詢。 – Phil