如何显示显示categoory_id = id

时间:2019-02-03 15:20:33

标签: laravel-5.7

我们有三个街区

Block

和三个类别:

  • 重要链接
  • 用户链接
  • 联系

    选择并保存Userful Links类别时,我想在块中显示Userful Links

我想显示categoory_id = id

的类别

控制器

public function index ()
{
    $id = 1;
    $webDesigns = About::with(['categories' => function($q) use ($id) {
        $q->where('category_id', $id);
    }])->get();
    return $webDesigns;
    $webDesigns = About::with(['categories' => function($q) { $q->where('id', 1); }])->get();
    $developers = About::with(['categories' => function($q) { $q->where('id', 2); }])->get();
    $graphics = About::with(['categories' => function($q) { $q->where('id', 3); }])->get();
    $computers = About::with(['categories' => function($q) { $q->where('id', 4); }])->get();
    return view('Home.index', compact('webDesigns', 'developers', 'graphics', 'computers'));
}

型号

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

Schema::create('about_category', function (Blueprint $table) {
    $table->integer('about_id')->unsigned();
    $table->integer('category_id')->unsigned();

    $table->foreign('about_id')->references('id')->on('abouts')->onDelete('cascade');
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});

Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('abouts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});

如何工作?还是我想念什么?

0 个答案:

没有答案