Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

Christophvh

I Followed a tutorial with this source code: https://github.com/laracasts/Dedicated-Query-String-Filtering/tree/master/app

If you have laracasts you can watch the video here

What i like to achieve is filter products based on their category.

When i filter on the product itself , it works fine

class ProductFilter extends QueryFilter

{
    public function categorie($name)
    {
        return $this->builder->where('name' , $name);
    }

}

But when i try to filter on the relationship it doens't work. (i get no errors either) . The error is located in this file , i think

class ProductFilter extends QueryFilter

{
    public function categorie($name)
    {
        return $this->builder->categories()->where('name' , $name);
    }

}

View

<form  method="get" action="/producten/categorie" style="display:inline-block">
 @foreach($roots as $root)
   <li><button type="submit" name="categorie" value="{{$root->name}}" class="button-link">{{$root->name}}</button></li>
 @endforeach
</form>

Route

Route::get('producten/categorie' , 'FrontProductController@index');

FrontProductController

  public function index(ProductFilter $filters)
    {
        Product::filter($filters)->get();

    }

QueryFilter class

abstract class QueryFilter
{
    protected $request;
    protected $builder;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function apply(Builder $builder)
    {
        $this->builder = $builder;

        foreach ($this->filters() as $name => $value) {
            if (! method_exists($this, $name)) {
                continue;
            }
            if (strlen($value)) {
                $this->$name($value);
            } else {
                $this->$name();
            }
        }
        return $this->builder;

    }

    public function filters()
    {
        return $this->request->all();
    }

}

Product Model

  public function categories()
  {
      return $this->belongsToMany('App\Category')->withTimestamps();
  }

 public function scopeFilter($query, QueryFilter $filters)
   {
       return $filters->apply($query);
   }
Christophvh

In the product filter i need to do the following for many-to-many relationships:

public function category($name)
    {
        return $this->builder->whereHas('categories', function ($query) use ($name) {
            $query->where('name', $name);
        });
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Building a dynamic query of a many-to-many relationship in Laravel 5

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Many to Many relationship query. Laravel

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Laravel many-to-many relationship query

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

SQLAlchemy: filtering count in many-to-many relationship query

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel 5.2 Many to Many Relationship inverse query empty results

From Dev

many-to-many relationship OrderBy - Laravel query builder

From Dev

Laravel - SQL query equals the value of id to null in many to many relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

Laravel 5 - access specific model on many to many relationship

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Laravel's whereHas not filtering fetched results for a relationship (one-to-many)

From Dev

Laravel's whereHas not filtering fetched results for a relationship (one-to-many)

From Dev

Laravel 6 many to many relationship

From Dev

Pagination with many to many relationship in laravel

From Dev

Laravel many to many relationship error

From Dev

Laravel many to many relationship with conditions

Related Related

  1. 1

    Building a dynamic query of a many-to-many relationship in Laravel 5

  2. 2

    Laravel Many to Many query relationship with where clause

  3. 3

    Many to Many relationship query. Laravel

  4. 4

    Laravel Many to Many query relationship with where clause

  5. 5

    Laravel many-to-many relationship query

  6. 6

    Laravel 5 Eloquent count many to many relationship

  7. 7

    Laravel 5 | Many to Many Relationship not working

  8. 8

    SQLAlchemy: filtering count in many-to-many relationship query

  9. 9

    Laravel - Many to Many relationship

  10. 10

    Many to many relationship in Laravel

  11. 11

    Many to Many relationship with Laravel

  12. 12

    Laravel 5.2 Many to Many Relationship inverse query empty results

  13. 13

    many-to-many relationship OrderBy - Laravel query builder

  14. 14

    Laravel - SQL query equals the value of id to null in many to many relationship

  15. 15

    Laravel 5 Eloquent Relationship - Has Many Though

  16. 16

    Laravel 5 Deleting a one-to-many relationship

  17. 17

    Laravel 5 one to many eloquent relationship

  18. 18

    Laravel 5 Eloquent Relationship - Has Many Though

  19. 19

    Query many to many relationship with DetachedCriteria

  20. 20

    how to query a many to many relationship?

  21. 21

    eloquent: query many to many relationship

  22. 22

    Laravel 5 - access specific model on many to many relationship

  23. 23

    Empty data returning from many to many relationship laravel 5

  24. 24

    Laravel's whereHas not filtering fetched results for a relationship (one-to-many)

  25. 25

    Laravel's whereHas not filtering fetched results for a relationship (one-to-many)

  26. 26

    Laravel 6 many to many relationship

  27. 27

    Pagination with many to many relationship in laravel

  28. 28

    Laravel many to many relationship error

  29. 29

    Laravel many to many relationship with conditions

HotTag

Archive