I have a page that shows a shop with all its relations that are:
- completed and incomplete products (status 1 means completed)
- products photos (product_id in products_photos table)
- likes count for each product (morphmany)
- followers count and button for the shop (many to many with dedicated model ShopFollower )
with that in mind I have the following query in my ShopsController:
public function show($shop)
{
$shop = Shop::findByUrl($shop);
$shop->load(['products' => function ($query) {
$query->orderBy('created_at', 'desc');
}, 'followers', 'products.photos', 'products.likes']);
$completeProducts = $shop->products->filter(function ($product) {
return $product->status == 1;
});
$incompleteProducts = $shop->products->filter(function ($product) {
return $product->status == 0;
});
return view('shops.show', compact('shop', 'completeProducts', 'incompleteProducts'));
}
and here is my view that I show the shop and its list of products (I removed most of the html and just copied the calls and echos):
@section('content')
<!-- Here is the shop info-->
<img src="{{ $shop->logo }}" style="
width:128px;
height:128px;
border-radius:50%;">
{{ $shop->name }}
{{ '@' . $shop->url }}
<follow-button
:is-followed="{{ json_encode($shop->isFollowed) }}"
shop-url="{{ $shop->url }}"
hide="{{ Auth::check() && Auth::user()->is($shop->user) }}"
logged-in="{{ Auth::check() }}"
></follow-button>
<shop-stats
:products-count-prop="{{ $shop->products->count() }}"
:followers-count-prop="{{ $shop->followers->count() }}"
></shop-stats>
<span class="">
{{ App::isLocale('en') ? $shop->location_en : $shop->location_ar }}
</span>
{{ $shop->description }}
<a href="{{ $shop->website }}" target="_blank">Website</a>
<a href="{{ 'https://www.instagram.com/' . $shop->instagram }}" target="_blank">
Instagram
</a>
@can ('createProduct', $shop)
<addproduct
url="{{ $shop->path() }}/products"
lang="{{ App::getLocale() }}">
{{ csrf_field() }}
</addproduct>
@endcan
</section>
<!-- Here is the list of products-->
@foreach ($completeProducts as $product)
<h3 class="marginless">
{{ App::isLocale('en') ? $product->name_en : $product->name_ar }}
</h3>
<productlink
url="{{ $product->path }}"
product-id="{{ $product->hashedid }}"
></productlink>
@if ($product->likescount)
<i class="fa fa-1x fa-heart" aria-hidden="true"></i>
{{ $product->likescount }}
@endif
{{ App::isLocale('en') ? $shop->currency->code_en : $shop->currency->code_ar }}
{{ $product->formatted_price }}
@if ($product->photos->first()['tn_url'])
<img src="{{ $product->photos->first()['tn_url'] }}" class="img-responsive">
@endif
@endforeach
@endsection
With this code I have 11 queries running As the following:
select * from `shops` where `url` = 'kris.brook' limit 1
select * from `products` where `products`.`shop_id` in ('1') order by `created_at` desc
select * from `product_photos` where `product_photos`.`product_id` in ('1', '2', '3', '5', '6', '8')
select * from `likes` where `likes`.`liked_id` in ('1', '2', '3', '5', '6', '8') and `likes`.`liked_type` = 'App\Product'
select * from `shop_followers` where `shop_followers`.`shop_id` in ('1')
select * from `users` where `id` = '2' limit 1
select exists(select * from `shop_followers` where `shop_followers`.`shop_id` = '1' and `shop_followers`.`shop_id` is not null and `user_id` = '2') as `exists`
select * from `users` where `users`.`id` = '1' limit 1
select * from `cities` where `cities`.`id` = '1' limit 1
select * from `countries` where `countries`.`id` = '1' limit 1
select * from `currencies` where `currencies`.`id` = '1' limit 1
Is that Ok or I need to use other methods like the query builder and join the tables?
Thanks in advance