[英]retrieve data from multiple table
我有兩個表(用戶和角色)具有一對一關系用戶模型
public function user_role()
{
return $this->hasOne('App\Role');
}
控制器中的功能
public function update_role($id)
{
$role = User::with('user_role')->find($id);
return view('update_role') -> with ('role',$role);
}
update_user視圖
{{$role -> name}}
{{$role -> setting}}
{{$role -> images}}
但是檢索到的數據只是用戶表中的用戶名,而不是角色表中的用戶設置和用戶圖像
嘗試這個
{{$role->user_role->name}} //this is if you are getting the role name
{{$role->user_role->setting}} //this is if you are getting the role setting
{{$role->user_role->images}} //this is if you are getting the role images
更新於2015-11-23
嘗試在控制器中更改變量的名稱,以便在我們將其帶到視圖時具有正確的含義,因為您正在為用戶帶來user_role。
//controller
$user= User::with('user_role')->find($id);
return view('update_role') -> with ('user',$user);
//view - accessing it will be as simple as
Username {{$user->username}}<br> //accessing column in your user table
Role {{$user->user_role->name}}<br> //accessing column in your user_role
Setting {{$user->user_role->setting}}<br>
Image {{$user->user_role->images}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.