Eloquent 远程多对多该如何快速获取数据?

教程中提到远程一对多,可直接获取数据。
对于远程多对多该如何处理呢?
比如:

class User
{
    // 用户标签
  public function labels()
 {  
     return $this->belongsToMany('App\Models\CustomerLabel', 'customer_label_relations', 'user_id', 'customer_label_id');
 }
}
class CustomerLabel extends Model
{
  // 用户
  public function users()
 {  
     return $this->belongsToMany('App\User', 'customer_label_relations', 'customer_label_id', 'user_id');\
 }
}
class CustomerFunction extends Model
{
  public function labels()
 { 
     return $this->belongsToMany('App\Models\CustomerLabel', 'customer_label_functions', 'customer_function_id', 'customer_label_id');\
 }
}

如何快速获取某个$user的相关的CustomerFunction呢?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 2

没看懂你的关联关系。假设,你是这样的多对多关系。user - label - function。 通过 user 获取 function 的话,可以这样操作。

User::with('label.function')->get();

如果想要纯净的 function 列表,需要自己遍历摘出。

3年前 评论

$user->label->pluck('function')->flatten(); 试下这个,老铁

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!