【发布时间】:2020-09-09 05:12:06
【问题描述】:
我需要根据其类别 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 Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function products()
{
return $this->hasMany(Product::class);
}
}
这是我的产品模型。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Storage;
class Product extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
当我选择特定类别时,我的 URL 将我引导至该 category_id 的页面,但数据未通过。可能是什么问题呢?有人请指导我吗?
【问题讨论】:
标签: php laravel e-commerce