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

captainblack

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

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:

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

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

fetch join table data using eloquent laravel

How to join three table by laravel eloquent model

How to alias a table in Laravel Eloquent queries (or using Query Builder)?

How to query pivot table with multiple foreign keys Laravel 5 Eloquent

How to update a pivot table using Eloquent in laravel 5

How do I write the following SQL query in Laravel using Eloquent?

Laravel Eloquent - how to join a table

How do I do a create table query with Eloquent outside of Laravel?

Laravel : Join within query using Eloquent

Laravel join table using eloquent

Eloquent Query with Pivot Table

How to output results using a pivot table in the database query? Laravel

how to join multiple table on laravel eloquent

Laravel 4.2 Eloquent using lists() with a join query

How to filter Laravel 5 eloquent query using entrust

How do I query a polymorphic pivot table with Eloquent & Laravel 5

Laravel join query using Laravel eloquent

laravel eloquent pivot table query "ambiguous column name"

Eloquent query using join to retrieve pivot table and related data

How to join three table by laravel eloquent model Laravel 5

How to perform a join containing a sub query on an Eloquent Relationship in laravel 5

How to do a multiple select with raw query using Eloquent in Laravel 5.1

Laravel eloquent with pivot table

Laravel Eloquent - Pivot Table

Laravel Eloquent ManyToMany with Pivot, secondary query with variables in pivot table?

How to join Table into Table in Laravel Eloquent

Laravel Eloquent Join Using Another table

How to query count on the pivot table in Laravel eloquent

Perform multiple pivot table join query perform SUM using Laravel Join