John Davidson

php - Show Products From specific category in Laravel


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 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">
<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>
<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 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>

Here Is PDF link of my Phpmyadmin
Structure Tab PDF

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:

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

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

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

3 of 5 based on 1464 votes


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