I'm building a search by company and/or category. The output should be listing companies, each & every company output with 1 or more categories.
Example output to achieve:
- Company 1, (category 1, category 2,...)
- Company 2, (Category 1, category...) ...
Company entry about 20k rows while categories 1k+ rows.
this query taking too long time & eat up processors as well.
$companies = Companies
::with(['categories'=>function($q)use($category){
if (isset($category)){
$q->where('slug','=',$category->slug);
}
}])->paginate(20);
While this query, only filter the category inside the company modal.
$companies = Companies::whereHas('categories',function($q)use($category){
$q->where('categories.id','=',$category->id);
})
->paginate(20);
With 2 modals
Companies
public function categories(){
return $this->belongsToMany('App\Models\Categories','companies_categories','company_id','category_id');
}
Categories
public function companies(){
return $this->belongsToMany('App\Models\Companies','companies_categories','category_id','company_id');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire