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

NKHO's avatar
Level 2

HOLY COW!!

It friggin worked:

I added to my announcement model

public function testXY(){
    return $this->hasMany('App\Attachment');
  }

and replaced 'attachments' with testXY in my query like so:

$posts =       Announcement::with('comments', 'testXY')
                     ->orderBy('id', 'desc')
                     ->get();

And it echoed test 5 times, the number of attachments on that post. AWESOME! Interestingly, it echoes it regardless of whether I put 'testXY' or 'attachments' in the query. Looks like it accepts the class name or the name of the method in the model.

You are awesome!!!

Previous

Please or to participate in this conversation.