Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

How to display category where id = 1?

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?

0 likes
10 replies
Sergiu17's avatar
$abouts = About::all(); // collection
$webDesigns = $abouts->categories()->where('id', '1'); // This will work only with one instance

// example
$about = About::find(1);
$webDesigns = $about->categories()->where('id', '1'); // Now this should work
// something like this
$id = 1;
$webDesigns = About::with(['categories' => function($q) use ($id) {
    $q->where('id', $id);
}])->get();
bobbybouwmann's avatar

So this is not correct code, because you have don't have an About object here but a Collection of About objects.

$abouts = About::all(); // Collection of About objects (basically an array)

// You're trying to perform categories() on a collection, which doesn't work
// Also ending the statement with `where('id', 1')` doesn't work on the query builder
// It would work on a collection though
$webDesigns = $abouts->categories()->where('id', '1');

So I'm not sure what you're trying to achieve, but I think you want the categories of a single About object right?

// Returns About object
$about = About::first(); 

// Returns Collection of Category
$webDesigns = $about->categories()->where('id', 1)->get(); 

// Alternatively you can use the following approach
$about = About::with(['categories' => function ($query) {
    return $query->id === 1;
}])->first();

$webDesigns = $about->categories;

I suggest you to read through the documentation one more time to understand this a bit better: https://laravel.com/docs/5.7/eloquent-relationships

Vilfago's avatar
public function index ()
{

    $webDesigns = About::with('categories' =>function($q) { $q->where('id', '1');}])->get();
    return view('Home.index', compact('webDesigns'));
}

1 like
mozew's avatar
Level 6

@BOBBYBOUWMANN - I see this error

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$id

Please or to participate in this conversation.