2016-12-05 41 views
0

無法從數據透視表獲得列數據這些都是我的模型獲取數據

產品

public function users() 
    { 
    return $this 
     ->belongsToMany('App\User') 
     ->withTimestamps() 
     ->withPivot('auth_product'); 
    } 

用戶

public function products() 
{ 
    return $this 
     ->belongsToMany('App\Products') 
     ->withTimestamps() 
     ->withPivot('auth_product'); 
} 

控制器

$authorized_users = Products::find($id)->pivot->auth_user; 

它給「嘗試獲得非客體的財產「。我試圖讓布爾值

編輯:Succeded中獲取價值,但現在給「空」值

$authorized_users = Products::find($id)->first()->pivot['auth_user']; 

回答

1
$authorized_users = Products::find($id)->pivot->auth_user; 

這是好的。確保您在產品表中具有該特定的$id

可以以第一名的成績查詢:

$authorized_users=Products::first()->pivot->auth_user; 
+0

它給空,但有值下的product_id(用作外鍵)該列,但它仍然拋出空 – OunknownO