I need to get all products according to its category id. This is my controller.
UserProductsController
class UserProductsController extends Controller
{
public function index()
{
$products = Product::get();
return view ('products')->with(compact('products'));
}
public function product_categories()
{
$categories = Category::all();
return view ('categories')->with(compact('categories'));
}
public function products(Category $category)
{
$category->load('products');
return view('categorize')->withCategory($category);
}
}
This is my blade file.
@foreach($category as $c)
<div class="mb-2" class="font-size-12 text-gray-5">{{ $c ['cat_name'] }}</a></div>
<h5 class="mb-1 product-item__title" class="text-blue font-weight-bold">{{ $c ['prod_name'] }}</a></h5>
<li class="line-clamp-1 mb-1 list-bullet">{{ $c ['prod_description'] }}</li>
<div class="text-gray-20 mb-2 font-size-12">{{ $c ['prod_item_code'] }}</div>
<div class="flex-center-between mb-1">
<div class="prodcut-price">
<div class="text-gray-100">LKR {{ $c ['prod_price'] }}.00</div>
</div>
</div>
</div>
@endforeach
This is my route file.
Route::get('/categorize/{category_id}', 'UserProductsController@products')->name('categorize');
This is the Categories table.
public function up()
{
Schema::create('categories', function (Blueprint $table)
{
$table->bigIncrements('id');
$table->unsignedBigInteger('parent_id')->nullable();
$table->string('cat_name')->nullable();
$table->string('cat_image_path')->nullable();
$table->timestamps();
});
}
This is my Products table.
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('prod_name');
$table->string('prod_brand')->nullable();
$table->unsignedBigInteger('category_id');
$table->string('prod_description')->nullable();
$table->string('prod_item_code')->nullable();
$table->string('prod_modal')->nullable();
$table->string('prod_size')->nullable();
$table->string('prod_weight')->nullable();
$table->string('prod_height')->nullable();
$table->string('prod_manufacturer')->nullable();
$table->float('prod_price')->nullable();
$table->float('prod_discount')->nullable();
$table->float('prod_quantity')->nullable();
$table->string('prod_image_path')->nullable();
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
This is my category model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function products()
{
return $this->hasMany(Product::class);
}
}
This is my product model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Storage;
class Product extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
When I select the particular category my URL is directing me to that category_id's page, but the data doesn't pass. What could be the problem? Anyone please direct me?
You can do that with eager loading. Eager loading is a concept in which when retrieving items, you get all the needed items together with all (or most) related items at the same time. This is in contrast to lazy loading where you only get one item at one go and then retrieve related items only when needed
Category model :
public function products()
{
return $this->hasMany(Product::class);
}
On your controller :
public function products(Request $request, $category_id)
{
$category= Category::with('products')->findOrFail($category_id);
return view('categorize')->with(compact('category'));
}