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 own category in own block with laravel?

We have three blocks

Block

And three categories:

  • Important Links
  • Userful Links
  • Get in touch

I want to display Userful Links in block when select and save Userful Links category.

I want to display categories where categoory_id = id

Controller

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

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

How to this work? OR What am I missing?

0 likes
26 replies
shez1983's avatar

i am not sure i understand what you are trying to do BUT You can do one query using $result = ....whereIn('id', [1,2,3,4]) and then $results->groupBy('id').. or a custom group by

shez1983's avatar

i was giving you side tip - not related to your q but telling you how you can cut down from 5 queries to ONE.. making ur code a bit more faster to run! sorry

Snapey's avatar

Come at this the other way.

Each Category belongsToMany About

Category model

    public function abouts()
    {
        return $this->belongsToMany(About::class);
    }

controller


    $categories = Category::with('about')->get();

    return view('Home.index', compact('categories'));

Now you can iterate over $categories and in each of those, iterate over the $abouts

or, use where to pick different categories

    @foreach($categories->where('id',1)->abouts as $about)

        // list $about details

    @endforeach


    @foreach($categories->where('id',2)->abouts as $about)

        // list $about details

    @endforeach


mozew's avatar
Level 6

Does anyone answer my questions?

Snapey's avatar

sorry typo here

$categories = Category::with('about')->get();

should be

$categories = Category::with('abouts')->get();
mozew's avatar
Level 6

I tried already about and abouts. I get this error.

Property [abouts] does not exist on this collection instance. (View: C:\xampp\htdocs\img\test\resources\views\Home\index.blade.php)

Property [about] does not exist on this collection instance. (View: C:\xampp\htdocs\img\test\resources\views\Home\index.blade.php)

Snapey's avatar

check the data returned from the query

mozew's avatar
Level 6

What do you mean? In which file? Controller Or Model Or Database ....

Snapey's avatar

Do you have any other options for career choice?

mozew's avatar
Level 6

Yes I have any other options for career choice... In database I have data in my database.

Snapey's avatar

This is a query

$categories = Category::with('abouts')->get();

what does it return?

dd($categories);

You learn this day #1

YeZawHein's avatar

@irankhosravi I think that code is ok.When you're using get() you get a collection. In this case you need to iterate over it to get properties:

@foreach ($collection as $object)
    {{ $object->title }}
@endforeach 

When you're using find() or first() you get an object, so you can get properties with simple:

{{ $object->title }}
YeZawHein's avatar

@irankhosravi I don't know what u exactly want but plz look About Controller

public function index ()
{
    $webdesigns = About::all();
  
    return view('Home.index', compact('webdesigns'));
}

View

@foreach ($webdesigns as webdesign)
{{$webdesign->name}}

//note:category have relation with many to many relation right?So
@foreach ($webdesign->categories as category)
{{$category->title}}
@endforeach 

@endforeach 
frezno's avatar

@IRANKHOSRAVI - very funny...

You are here asking for help and people are trying to help you, so no need to swear at them (snapey in this case)

Snapey's avatar

sorry, with two letters its barely possible to tell that its a link

Please or to participate in this conversation.