3
votes

i'm working in laravel 5.4, i have this tables:

PROJECT | id | tags | title |

PRODUCT | id | project_id | category_id |

CATEGORY | id | name |

The tags of project is a string separated by comma:

tag1,tag2,tag3...

The first step that i need is filter projects by termn:

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

it work well, but i would like get all product's project with a specific "category_id".

My relations models is working well.

Thank you for your help! this is the first time that i'm trying to do something like this.

1

1 Answers

3
votes

Use with() method:

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