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

thefuzzy0ne's avatar

Returning all results from table with pivot data for specific model

Hi, everyone!

I'm hoping someone can give me a simple solution to my problem. I'm sure it's really simple and I'm just overthinking it.

I have 3 tables.

loans
  - id
  - title

loan_marker
  - loan_id
  - marker_id

markers
  -marker_image

What I'd like to be able to do is return ALL markers from the markers table, but with the pivot data for a specific loan. So let's say I have 10 markers and 2 have been applied to a loan. I'd like to get a list of all markers, two of which should have some pivot data so I can see that they're selected for that loan.

The main reason I want to do this, is because I have a vue component. Rather than making two requests to the server (one for the list of markers and another to see what markers are selected for a given loan), I'd rather just do it in one hit. Ultimately, I'd like to return something like this:

[
    [
        'marker_image' => 'http://mysite.tld/img/marker_image1.png',
        'selected' => true,
    ],
    [
        'marker_image' => 'http://mysite.tld/img/marker_image2.png',
        'selected' => false,
    ],
    [
        'marker_image' => 'http://mysite.tld/img/marker_image3.png',
        'selected' => false,
    ],
];

And so on... Is there a way to do this while keeping all my hair firmly within my hair follicles?

0 likes
1 reply
jlrdw's avatar

@thefuzzy0ne sorry not an answer, but a question. Where were you, you went missing for a while. I missed you.

Please or to participate in this conversation.