Laravel 5.4 - Querying a relationship with a search term

I am working in laravel 5.4, I have these tables:

Project | id | tags | name |

Products | id | project_id | category_id |

CATEGORY | id | name |

Project tags are a comma separated string:

tag1, tag2 tag3 ...

The first step I need is the termn filter project:

$termn = 'examples';
$projects = Project::where('tags', 'LIKE' , '%'.$termn.'%')->paginate(20);

      

it works well, but I would like to get the whole product project with a specific "category_id".

My relationship models work well.

Thanks for the help! this is the first time i try to do something like this.

+3


source to share


1 answer


Use the with()

method:



Project::where('tags', 'like' , '%'.$termn.'%')
       ->with(['products' => function($q) use($categoryId) {
           $q->where('category_id', $categoryId);
       }])
       ->paginate(20);

      

+3


source







All Articles