使用Laravel Eloquent需要根据类别id获得所有产品



我需要根据其类别id获取所有产品。这是我的控制器。

用户产品控制器

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);
} 

}

这是我的刀片文件

@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

这是我的路由文件

Route::get('/categorize/{category_id}', 'UserProductsController@products')->name('categorize');

这是类别表。

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();
});
}

这是我的产品表。

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');
});
}

这是我的类别模型。

<?php
namespace App;
use IlluminateDatabaseEloquentModel;

class Category extends Model
{
public function products()
{
return $this->hasMany(Product::class);
}
}

这是我的产品型号。

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use Storage;
class Product extends Model
{
public function category()
{
return $this->belongsTo('AppCategory');
}
}

当我选择特定类别时,我的URL会将我引导到该category_id的页面,但数据没有通过。问题出在哪里?有人能告诉我吗?

您可以通过热切加载来实现这一点急切加载是一个概念,在检索项目时,您可以同时获得所有需要的项目以及所有(或大多数(相关项目。这与懒惰加载形成了鲜明对比,在懒惰加载中,你一次只得到一个项目,然后只在需要时检索相关项目

类别型号:

public function products()
{
return $this->hasMany(Product::class);
}

在您的控制器上:

public function products(Request $request, $category_id)
{
$category= Category::with('products')->findOrFail($category_id);
return view('categorize')->with(compact('category'));
}

我所理解的是,您在前端(可能是HTML(中有一些"category-id",并且您希望通过某种路由"/category/{category_id}"将该id传递给您的控制器,并获得与之相关的所有产品。因此,您必须像下面这样重构您的控制器方法。

public function products(int $category_id)
{
$products = Product::where('category_id',$category_id)->get();
return view ('categorize')->with(compact('products'));
} 

如果你有任何不同的需求,请随时询问并详细说明你的问题。我一定会帮你的。

当你在路线中提到category_id时,所以在控制器中你需要进行

public function products(Category $category_id) // parameter name should match with router param
{
$category_id->load('products');
$category  = $categoryid;
return view('categorize')->withCategory($category);
} 

或者正确的方法

Route::get('/categorize/{category}', 'UserProductsController@products')->name('categorize');

category_idcategory,然后您的路由模型绑定将工作

ref链接https://laravel.com/docs/7.x/routing#route-模型绑定

如果你想使用路由模型绑定,你必须这样定义你的路由:

Route::get('/categorize/{category}', 'UserProductsController@products')->name('categorize');

所以在这种情况下,$category实际上是一个模型,而不是一个集合。因此,您目前拥有的foreach没有意义。应该是这样的:

@foreach($category->products as $product) 
<div>{{ $product->prod_name }}</div>
...
@endforeach

最新更新