John Davidson

php - Show Products From specific category in Laravel

0 comments
Message:


I just want to show product with "Rumored" table can someone help how can I show them in my side bar? below is my sidebar code. And side bar Already show 12 Latest Products from all categories it must show only 8 product and with "Rumored" category. not from all categories.


<div class="heading mb-2">
<h3>Rumored Phones</h3>
</div>

<div class="row">
@foreach ($popular_devices as $device)
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-6 col-6">
<div class="device-box">
@if($device->image)
<a title="{{ $device->brand.' '.$device->model }} " href="{{ route('device', ['brand_slug' => $device->brand_slug, 'slug' => $device->slug]) }}">
<img src="{{ asset("uploads/$device->image") }}" alt="{{ $device->brand.' '.$device->model }}" class="img-fluid"></a>
@endif
<a class="title" title="{{ $device->brand.' '.$device->model }}" href="{{ route('device', ['brand_slug' => $device->brand_slug, 'slug' => $device->slug]) }}">{{ $device->brand.' '.$device->model }}</a>
</div>
</div>
@endforeach
</div>

<div class="text-center mb-4">
<a class="btn btn-light btn-block" href="{{ route('devices') }}" title="All devices">View All {{ $count_all_devices }} Devices</a>
</div>

Here Is PDF link of my Phpmyadmin
https://drive.google.com/file/d/1ZWgq6quXID0jTB8kQ5aj7v4maacPeSRY/view?usp=sharing
Structure Tab PDF
https://drive.google.com/file/d/1i_q0m1o5blsVemslugfWCKMriW-W3P0C/view?usp=sharing



My Answer:
specific category in Laravel, you can use the following code:

First, you need to define a relationship between your Product model and Category model. In your Product model, you can define a belongsTo relationship like this:

php
public function category()
{
return $this->belongsTo(Category::class);
}


Then, in your controller, you can retrieve products from a specific category like this:

php
use App\Models\Product;

public function showProductsFromCategory($category_id)
{
$products = Product::where('category_id', $category_id)->get();

return view('products.index', compact('products'));
}


In this code, we are retrieving all products that belong to a specific category with the given category_id. You can then pass these products to your view and display them as needed.

Make sure to replace 'Category' with the actual name of your Category model and adjust the column names according to your database schema.

Rate this post

4 of 5 based on 9204 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us