【问题标题】:Laravel 5: How to do a join query on a pivot table using Eloquent?Laravel 5:如何使用 Eloquent 对数据透视表进行连接查询?
【发布时间】:2017-05-05 06:30:51
【问题描述】:

我的 Laravel 应用程序中有 2 个表,即 customers 和 stores。客户可以属于许多商店,商店可以有很多客户。它们之间有一个数据透视表来存储这种关系。

问题是,如何使用 Eloquent 提取给定商店的客户列表?可能吗?我目前能够使用 Laravel 的 Query Builder 提取它。这是我的代码:

| customers | stores      | customer_store |
-------------------------------------------
| id        | id          | customer_id    |
| name      | name        | store_id       |
| created_at| created_at  | created_at     |
| updated_at| updated_at  | updated_at     |

客户模型:

public function stores(){
        return $this->belongsToMany(Store::class)
            ->withPivot('customer_store', 'store_id')
            ->withTimestamps();
    }

店铺模式:

public function customers(){
        return $this->belongsToMany(Customer::class)
            ->withPivot('customer_store', 'customer_id')
            ->withTimestamps();
    }

数据库查询(使用查询生成器):

$customer = DB::select(SELECT customers.id, customers.name, customers.phone, customers.email, customers.location FROM customers LEFT JOIN customer_store on customers.id = customer_store.customer_id WHERE customer_store.store_id = $storeID);

【问题讨论】:

  • 直截了当DB:select 不是Query Builder。如果您希望获得Store 的客户,您可以使用$current_store->customers(),这将获取属于商店的所有客户,因为$current_store 是模型实例而不是集合。如果您想检索多个商店并获取其相关客户,您可以使用@kapil.dev 回答。
  • 在旁注中,您的->withPivot() 实际上是错误的,read the docs,您可以在关系本身上定义关系键,即。 ->belongsToMany(Customer::class, 'customer_store', 'store_id', 'customer_id')
  • @Bagus Tesa ...你让我的一天为我工作 belongsToMany(Customer::class, 'customer_store', 'store_id', 'customer_id') ......... ........谢谢..!!

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

试试这个:

public function result(Request $request) {

    $storeId = $request->get('storeId');

    $customers = Customer::whereHas('stores', function($query) use($storeId) {
        $query->where('stores.id', $storeId);
    })->get();

}

【讨论】:

    【解决方案2】:

    尝试执行这个...

    $result = Customer::with('stores')->get();
    

    希望这会有所帮助。

    要了解更多有关 Eloquent 关系的信息,请参阅:https://laravel.com/docs/5.1/eloquent-relationships

    【讨论】:

    • 我在实现您的代码后收到此错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_store.customer_store' in 'field list' (SQL: select stores.* , customer_store.customer_id as pivot_customer_id, customer_store.store_id as pivot_store_id, customer_store.customer_store as @9876543@32@, created_at, created_at.@9876 987654336@.updated_at as pivot_updated_at from stores inner join customer_store on stores.id = customer_store.store_id 其中customer_id.@9825 in (987654346@.@9876 ))
    • 显然,看看我的评论,看看你的帖子。但是,->with() 假定您正在加载 Customers 的集合
    • 没错!! @captainblack 你应该参考 BagusTesa 的评论,他解释得很好。
    • @kapil.dev 你在哪里传递'store_id'。
    • @Anish688 它应该映射到数据透视表中,它会给出属于该客户的映射商店......如果概念仍然不清楚,请阅读文档......
    【解决方案3】:

    试试下面:

    Customers 是您的型号,$storeID 是您的商店 ID。 $storeID 不在您的回调范围内。所以你必须使用 use 语句来传递它们。

    Customers::leftJoin('customer_store', function($join) use($storeID){
      $join->on('customers.id', '=', 'customer_store.customer_id')
      ->where('customer_store.store_id','=', $storeID);
    })
    ->whereNotNull('customer_store.store_id')//Not Null Filter
    ->get();
    

    希望对您有所帮助!

    【讨论】:

    • 是不是有点过于复杂了? op 希望它尽可能雄辩地我如何使用 Eloquent 提取给定商店的客户列表? 也许详细说明你为什么给出这个答案,将是最好的。
    • 嘿@AddWeb,你的代码有点工作,但它也会返回所有客户(不属于该商店的客户)。
    • 是的,基于 OP 的 DB 查询(使用查询生成器) 点。我尝试使用提前加入来提供最佳解决方案。
    • @captainblack 好的,将提供解决方案。果酱..!
    • 这个可行,但是,我觉得解决方案应该像 Eloquent 查询一样易于阅读。
    猜你喜欢
    • 2015-12-28
    • 2018-12-08
    • 1970-01-01
    • 2018-11-11
    • 2015-06-24
    • 2015-09-30
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多