如何在菜单和子菜单中导入类别?

时间:2020-07-19 20:52:59

标签: laravel

我想创建一个megamenu,我想将类别导入菜单和子菜单进行链接。

我有两个表categoriesknowledge_rooms。并且我误用了易拉包,我的laravel版本是5.8

类别表

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name_fa');
        $table->integer('parent_id');
        $table->timestamps();
    });
}

knowledge_rooms表

public function up()
{
    Schema::create('knowledge_rooms', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');
        $table->string('lang');
        $table->string('slug');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });

    Schema::create('category_knowledge_room', function (Blueprint $table) {
        $table->bigInteger('category_id')->unsigned();
        $table->bigInteger('knowledge_room_id')->unsigned();
        $table->primary(['category_id', 'knowledge_room_id']);
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->foreign('knowledge_room_id')->references('id')->on('knowledge_rooms')->onDelete('cascade');
    });
}

AppSeerviceProvider.php

public function boot()
{
    view()->composer('*', function($view) {
        $view->with('menus', Category::with('knowledgeRooms')->whereParent_id(0)->get());
    });
}

Category.php

public function getChild ()
{
    return $this->hasMany(Category::class, 'parent_id', 'id');
}

public function knowledgeRooms()
{
    return $this->belongsToMany(KnowledgeRoom::class);
}

header.blade.php

<div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ml-auto">
        @foreach($menus as $menu)
            {{--@continue($menu->parent_id !=0)--}}
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="fas {{ $menu->icon }} fa-2x"></i>
                    <span>{{ $menu->name_fa }}</span>
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    @foreach($menu as $submenu)
                        @continue($submenu->parent_id == 0)
                        @foreach($submenu->knowledgeRooms as $knowledgeRoom)
                            {{--@if($submenu->parent_id == $menu->id)--}}
                            <a class="nav-link" href="{{ $knowledgeRoom->path() }}">
                                <span>{{ $submenu->name_fa }}</span>
                            </a>
                            {{----}}
                            {{--<a class="dropdown-item" href=">--}}
                            {{--{{ $submenu->name_fa }}--}}
                            {{--</a>--}}
                            {{--@endforeach--}}
                            {{--@endif--}}
                        @endforeach
                    @endforeach
                </div>
            </li>
        @endforeach
    </ul>
</div>

web.php

Route::get('/knowledge-rooms/{knowledge-roomSlug}', 'KnowledgeRoomController@single');

KnowledgeRoom.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class KnowledgeRoom extends Model
{
    use Sluggable;

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function path()
    {
        return "/knowledge-rooms/$this->slug";
    }

    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

举一个例子,我以后会解决我的问题的。

我收到此错误

error

0 个答案:

没有答案