// for clarity I suggest renaming your relation to plural on ProductCategory model:
public function products()
{
return $this->hasMany('Product','category');
}
// although it doesn't change how the things work
// then this way you get the category and related products
$category = ProductCategory::with('products')->where('ref_title','=',$idcag)->first(array('name','id'));
$category->products; // Collection of Product models
// which is basically the same as this, as long as you get only one category first:
$category = ProductCategory::where('ref_title','=',$idcag)->first(array('name','id'));
$products = $category->products;
// Now these lines do exactly the same:
$products = $category->products; // Eloquent will know that it will need to run this:
$products = $category->products()->get(); // if it wasn't retrieved before
So the only thing you shouldn't have done is
ProductCategory::find($datas->id)
since this queries productcategories table once again.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community