dongli9894 2017-01-24 00:28
浏览 97
已采纳

如何从数据透视表中找到所有id pivot? (laravel 5.3)

I have a pivot table. The name is users_banks table. The table have field id, user_id, bank_id, status, account_name and account_number

Now, the pivot have 3 records

I display contain from pivot table using code like this :

$user_id = auth()->user()->id;
$user = $this->user_repository->find($user_id);

$account = array();
foreach($user->banks as $bank) {
    $account[] = $bank;
}

dd($account);

The result display all record in the form of array. There exist value of field user_id, bank_id, status etc. But I don't find value of id

How can I find id from pivot table?

  • 写回答

1条回答 默认 最新

  • douque9815 2017-01-24 00:46
    关注

    If the relationship is defined correctly you can use:

    $bank->pivot->id
    

    but make sure the user belongsToMany(Bank::class) and the bank belongsToMany(User::class) or else it wont work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器