繁体   English   中英

从数据透视表中的额外列获取数据

[英]Getting data from extra column in pivot table

无法从数据透视表中获取列数据这是我的模型

产品展示

 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;

它给出了“试图获取非对象的属性”。 我正在尝试获得布尔值

编辑:成功获得价值,但现在赋予“空”价值

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

这可以。 确保在产品表中具有该特定的$id

您可以检查第一条记录:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM