【问题标题】:How to formulate navbar link to its pages in laravel?如何在laravel中制定导航栏链接到其页面?
【发布时间】:2020-12-28 13:27:45
【问题描述】:

我需要制定我的网站导航栏并将其链接到页面。这是一个电子商务网站。每个菜单都有子菜单。这些菜单和子菜单需要根据给定的表格制定。由于这是我的第一个项目,因此有点难以理解如何整理它。

这意味着,我的菜单需要加载为台式机、笔记本电脑、打印机和扫描仪等。并且作为笔记本电脑的子菜单hp,而 dell 需要使用该 category_id 和 parent_id 加载。

这是我的刀片文件。

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

【问题讨论】:

    标签: php laravel navigation e-commerce menubar


    【解决方案1】:

    你需要循环$categories然后$categories-&gt;products

    在您的控制器中

    $categories= Category::with('products')->find($cat_name);
    return view('index',compact('categories'));
    

    在你的刀片中

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

    【讨论】:

    • 它没有显示任何内容。我在问题中添加了更多细节,请看一下。而且我还需要显示类别及其子类别,而不是其产品。 @Kamlesh 保罗
    • @Zeenath 然后你需要为parentchild 建立关系然后你可以循环
    • 在表中创建关系是否正确?公共函数 categories_id() { return $this->hasMany('category_id','parent_id'); } 公共函数 parent_id() { return $this->hasMany('category_id','parent_id'); } @Kamlesh 保罗
    • public function parent() { return $this->belongsTo(self::class); } 公共函数 children() { return $this->hasMany(self::class, 'parent_id');我使用此代码关联父母和孩子。希望是正确的!! @Kamlesh 保罗
    • 嵌套的 foreach 将如何变化?@Kamlesh Paul
    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 2023-03-11
    • 2018-07-03
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2014-02-16
    相关资源
    最近更新 更多