Laravel 5:如何使用 Eloquent 对数据透视表进行连接查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41235577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:57:46  来源:igfitidea点击:

Laravel 5: How to do a join query on a pivot table using Eloquent?

phpmysqllaravellaravel-5eloquent

提问by captainblack

I have 2 tables in my Laravel application namely customersand stores. Customers can belong to many stores and stores can have many customers. There's a pivot table between them to store that relation.

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

Question is, how do I extract the list of customers for a given store using Eloquent? Is it possible? I am currently able to extract that using Laravel's Query Builder. Here is my code:

问题是,如何使用 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     |

Customer Model:

客户型号:

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

Store Model:

店铺型号:

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

DB Query (using Query Builder):

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

$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);

回答by Alex Lam

Try this one:

试试这个:

public function result(Request $request) {

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

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

}

回答by kapil.dev

Try executing this...

尝试执行这个...

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

Hope this helps.

希望这可以帮助。

To know more about Eloquent Relationship refer: https://laravel.com/docs/5.1/eloquent-relationships

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

回答by AddWeb Solution Pvt Ltd

Try below:

试试下面:

Here Customersis your model and $storeIDis your store id. $storeIDis outside of the scope of your callback. So you must use the use statement to pass them.

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();

Hope this help you!

希望这对你有帮助!