How to display category where id = 1?

Posted 6 months ago by irankhosravi

I want to display categories where id = 1

Controller

public function index ()
{
    $abouts = About::all();
    $webDesigns = $abouts->categories()->where('id', '1');
    return view('Home.index', compact('webDesigns'));
}

Model

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

Table(s)

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();
});

But I get this error

Method Illuminate\Database\Eloquent\Collection::categories does not exist.

How to this work? OR What am I missing?

Please sign in or create an account to participate in this conversation.