2016-12-05 39 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