Reputation: 613
I am trying to fetch data using slug between 2 table, When I am using Eloquent then everything is fine but How to convert that query in Query Builder.
Eloquent Query :
$results = Product::orderBy('id','desc')->with('categories')->whereHas('categories', function ($query){
$query->where('slug', request()->sub_category);
})->paginate(24);
Here I am using two tables product and categories.
Query Builder :
$results = DB::table('products')
->leftJoin('wishlists', 'products.product_id', '=', 'wishlists.product_id')
->select('products.*', 'wishlists.wishlist_id', 'wishlists.user_id')
->with('menus')
->whereHas('categories', function ($query){
$query->where('slug', request()->category);
})
->orderBy('products.name', 'asc')
->paginate(24);
But here I am using 3 tables, Product, menu and Wishlist. Bcoz when user go to product page, I will highlight wishlisted product too.
Error :
Call to undefined method Illuminate\Database\Query\Builder::with()
Upvotes: 0
Views: 1536
Reputation: 239
You can use multiple with eloquent too,
try this
$results = Product::orderBy('id','desc')->with(['menus','categories' => function ($query){
$query->where('slug', request()->sub_category);
}])->paginate(24);
Upvotes: 1