Laravel Eloquent Many to Many Query

user3260759

Please help me make this query in eloquent. A business can have many categories.

SELECT b.* 
FROM   businesses b 
       INNER JOIN categorybusiness cb 
               ON b.id = cb.business_id 
       INNER JOIN category c 
               ON cb.category_id = c.id 
WHERE  b.location LIKE '%query1%' 
       AND b.location LIKE '%query2%' 
       AND c.name LIKE '%query3%' 
       AND c.name LIKE '%query4%' 

my tables are.. businesses - contain the location column and a pivot table for category and business..

UPDATE: so i used this query...

$business5 = Business::WhereHas('categories', function($q) use($category,$query1)
    {
          $q->whereRaw("name like '%$category%' or businesses.name like '%$category%' $query1");         
    })->get();

$query1 looks like this but in a loop.

     $query1 .= " and businesses.address1 like '%$string%'"; 

It's working fine but can someone help me make a "MATCH AGAINST" statement in eloquent from this.

The Alpha

For making an Eloquent query you need to setup relationship and to create a many-to-many relationship you need to build the relationship like this in both models:

The Business model:

class Business extends Eloquent {
    //...
    public function categories()
    {
        return $this->belongsToMany('Caregory');
    }
}

The Category model:

class Category extends Eloquent {
    //...
    public function businesses()
    {
        return $this->belongsToMany('Business');
    }
}

The Eloquent query (You already have a pivot table):

$businesses = Business::with(array('categories' => function($q) use ($query3, $query4) {
    $q->where('categories.name', 'LIKE', '%'. $query3 .'%')
      ->where('categories.name', 'LIKE', '%'. $query4 .'%');
}))->where('businesses.location', 'like', '%'. $query1 .'%')
   ->where('businesses.location', 'like', '%'. $query2 .'%')
   ->get();

To check the result just use dd($businesses); and examine the collection so you'll get the idea about how you can loop them in your view. Basically, $businesses will contain a collection and each $business model in the collection will contain another collection of $categories, so loop could be something like this:

@foreach($businesses as $business)
    {{ $business->propertyname }}
    @foreach($business->categories as $category)
        {{ $category->propertyname }}
    @endforeach
@endforeach

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel eloquent - Many to Many, select only matching the many table

From Dev

Laravel save many-to-many relationship in Eloquent mutators

From Dev

Laravel 4 Many to Many with Eloquent and checkboxes

From Dev

Laravel Eloquent: In many to many relationships the ORM is returning only one Object

From Dev

Laravel many to many selecting with Eloquent

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

Laravel Eloquent: How to select not attached records in many to many relationship?

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel Eloquent many to many how to add where?

From Dev

Laravel Eloquent: ordering Many-to-Many query- user favorites first

From Dev

Need help to structure Eloquent Many to Many relationship (laravel 5.1)

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Laravel Eloquent Many-to-Many query

From Dev

Laravel eloquent many to many to many model

From Dev

Many to Many Eloquent Relation

From Dev

Many to Many relationship query. Laravel

From Dev

Laravel Eloquent one to many

From Dev

Laravel Eloquent "Many-to-Many-to-One ?"

From Dev

Laravel Eloquent: ordering Many-to-Many query- user favorites first

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Convert a many to many raw sql statement to eloquent query builder

From Dev

eloquent: query many to many relationship

From Dev

Laravel many-to-many relationship query

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

Related Related

  1. 1

    Laravel eloquent - Many to Many, select only matching the many table

  2. 2

    Laravel save many-to-many relationship in Eloquent mutators

  3. 3

    Laravel 4 Many to Many with Eloquent and checkboxes

  4. 4

    Laravel Eloquent: In many to many relationships the ORM is returning only one Object

  5. 5

    Laravel many to many selecting with Eloquent

  6. 6

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  7. 7

    Laravel Eloquent: How to select not attached records in many to many relationship?

  8. 8

    Laravel many to many relationship using Eloquent

  9. 9

    Laravel Eloquent many to many how to add where?

  10. 10

    Laravel Eloquent: ordering Many-to-Many query- user favorites first

  11. 11

    Need help to structure Eloquent Many to Many relationship (laravel 5.1)

  12. 12

    Laravel 5 Eloquent Many to Many 2ndary table

  13. 13

    Laravel Eloquent Many-To-Many distinct

  14. 14

    Laravel Eloquent many to many filter on intermediate table

  15. 15

    Laravel 5 Eloquent count many to many relationship

  16. 16

    Laravel Many to Many query relationship with where clause

  17. 17

    Laravel Eloquent Many-to-Many query

  18. 18

    Laravel eloquent many to many to many model

  19. 19

    Many to Many Eloquent Relation

  20. 20

    Many to Many relationship query. Laravel

  21. 21

    Laravel Eloquent one to many

  22. 22

    Laravel Eloquent "Many-to-Many-to-One ?"

  23. 23

    Laravel Eloquent: ordering Many-to-Many query- user favorites first

  24. 24

    Laravel Eloquent Many-To-Many distinct

  25. 25

    Laravel Many to Many query relationship with where clause

  26. 26

    Convert a many to many raw sql statement to eloquent query builder

  27. 27

    eloquent: query many to many relationship

  28. 28

    Laravel many-to-many relationship query

  29. 29

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

HotTag

Archive