Всем привет, не могу вывести цены по убыванию Мой контроллер
public function category(Category $category){
$categories = Category::where('active', 1)->get();
$products = Product::orderBy('price', 'desc')->get();
// dd($products);
// dd($category->$products);
return view('pages.products', compact('category', 'categories', 'products'));
}
Мой шаблон:
<div class="product_sidebar flex">
@if($category->products)
@if($category->products->count())
@foreach($category->products as $product)
<div class="product_card">
<a href="{{ route('category.index3', [$product->category, $product]) }}" class="pop_pro">
<div class="product_item">
<div class="flex column"><span class="code_num">{{ $product->article }}</span>
<style>
img.product-img {
width: 100%;
height: 200px;
}
</style>
<img src="{{ Voyager::image($product->image) }}" alt="" class="product-img"></div>
<div class="prod_info">
{{--<span>{{ $product->title }}</span>--}}
<h4>{{ $product->title }}</h4>
<div class="product_attr flex">
<div class="flex column char"><span class="attr_name">{{ $product->specification1 }}</span><span class="attr_quan">{{ $product->specification2 }}</span></div>
<div class="flex column char"><span class="attr_name">{{ $product->specification3 }}</span><span class="attr_quan">{{ $product->specification4 }}</span></div>
<div class="flex column char"><span class="attr_name">{{ $product->specification5 }}</span><span class="attr_quan">{{ $product->specification6 }}</span></div>
</div>
<div class="flex last_line">
<span class="price">{{ $product->price }} тг.</span>
<a data-fancybox href="{{ $product->youtube }}" class="youtube flex"><img src="{{ asset('build/img/youtube_icon.svg') }}"><span>Видео</span></a>
</div>
<a href="#modal_window" class="fancybox_modal" data-product="{{ $product->title }}"><div class="btn2">Заказать</div></a>
</div>
</div>
</a>
</div>
@endforeach
@endif
@endif
Моя модель:
public function category()
{
return $this->belongsTo(Category::class);
}
Помогите
Попробуйте так:
public function category(Category $category){
$category->load(['products' => function($query) {
$query->orderBy('price', 'desc');
}]);
$categories = Category::where('active', 1)->get();
$products = Product::orderBy('price', 'desc')->get();
return view('pages.products', compact('category', 'categories', 'products'));
}
Продвижение своими сайтами как стратегия роста и независимости