问题描述
我需要制定网站导航栏并将其链接到页面。这是一个电子商务网站。每个菜单都有子菜单。这些菜单和子菜单需要根据给定的表格来制定。由于这是我的第一个项目,因此很难弄清如何整理它。
这意味着,我的菜单需要以台式机,笔记本电脑,打印机和扫描仪等加载,并以膝上型计算机 hp 的子菜单加载。 >,并且需要使用该category_id和parent_id加载 dell 。
这是我的刀片文件。
<div class="hs-mega-menu vmm-tfw u-header__sub-menu" aria-labelledby="basicMegaMenu">
<div class="row">
<div class="row u-header__mega-menu-wrapper">
<div class="col mb-3 mb-sm-0">
<span class="u-header__sub-menu-title">Desktops</span>
<ul class="u-header__sub-menu-nav-group mb-3">
<li>
<a class="nav-link u-header__sub-menu-nav-link" href="#" class="font-size-15 text-gray-90">HP</a>
</li>
<li>
<a class="nav-link u-header__sub-menu-nav-link" href="#" class="font-size-15 text-gray-90">DELL</a>
</li>
<li>
<a class="nav-link u-header__sub-menu-nav-link" href="#" class="font-size-15 text-gray-90">ACER</a>
</li>
<li>
<a class="nav-link u-header__sub-menu-nav-link" href="#" class="font-size-15 text-gray-90">ASUS</a>
</li>
</ul>
</div>
</div>
</div>
</div>
这是我的控制器。
HomeController
class HomeController extends Controller
{
public function index()
{
return view('home');
}
public function landing_page()
{
$features = Feature::get();
$products = Product::get();
return view ('index',compact('products','features'));
}
public function products(Request $request,$cat_name)
{
$category= Category::with('products')->find($cat_name);
return view('index')->with(compact('products'));
}
}
这是我的产品表。
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');
});
}
这是我的类别表。
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();
});
}
这是我的路线。
Route::get('/index','HomeController@categories')->name('index');
解决方法
您需要先循环$categories
,然后再循环$categories->products
在您的控制器中
$categories= Category::with('products')->find($cat_name);
return view('index',compact('categories'));
您的bldae中
<div class="hs-mega-menu vmm-tfw u-header__sub-menu" aria-labelledby="basicMegaMenu">
<div class="row">
<div class="row u-header__mega-menu-wrapper">
@foreach ($categories as $category)
<div class="col mb-3 mb-sm-0">
<span class="u-header__sub-menu-title">{{ $category->name }}</span>
<ul class="u-header__sub-menu-nav-group mb-3">
@foreach ($category->products as $product)
<li>
<a class="nav-link u-header__sub-menu-nav-link" href="#" class="font-size-15 text-gray-90">{{ $product->name }}</a>
</li>
@endforeach
</ul>
</div>
@endforeach
</div>
</div>
</div>