mercredi 3 janvier 2018

Laravel newQuery()

I've been looking at the newQuery eloquent model and best case uses and I can see the benefit in searching / filtering products based on a search page however is it possible to call the newQuery on only the Users related products?

For example, I have 2 models.

  • User
  • Product

User has many products and I have the relationship defined on the user model.

public function products() {
  return $this->hasMany('App\Product');
};

Now, previously if I wanted to filter all the products and take user out of the scenario I could use:

$query = (new \App\Product)->newQuery();

if($request->get('category')){
  $query->whereHas('category',function($q) use($request){
     $q->where('category_id',$request->get('category'));
  });
}

$products = $query->get();

This is great and I like this method and I now want to have a similar functionality on only users products.

For example, id like to:

$products = (Auth::user()->products)->newQuery();

if($request->get('category')){
  $products->whereHas('category',function($q) use($request){
     $q->where('category_id',$request->get('category'));
  });
}

$products = $query->get();

But I can't do this I get newQuery() method is not available.

Is there a better way of performing optional queries based on parameters?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire