Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save me7media/ce7061433aae165e5de64644513c9eae to your computer and use it in GitHub Desktop.
Save me7media/ce7061433aae165e5de64644513c9eae to your computer and use it in GitHub Desktop.
Получение товаров категории и ее дочерних категорий с условиями
$products = null;
Category::whereId($category_id)->with([
'products' => function ($q) use (&$products, $search) {
$products = $q->where('active', true)
->search($search) //Scope
->get()
->unique();
},
'childrens.products' => function ($q) use (&$products, $search) {
$products = $q->where('active', true)
->search($search) //Scope
->get()
->unique(); }
])->get();
//Scope
public function scopeSearch($query, $value)
{
return $query->where('name', 'LIKE', '%' . $value . '%')
->orWhere('full_name', 'LIKE', '%' . $value . '%')
->orWhere('code', 'LIKE', '%' . $value . '%')
->orWhere('article', 'LIKE', '%' . $value . '%');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment