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

kazehaya's avatar

haha thats brings me back to the drawing board again, this is almost the same way i started this conversation with you^^ Amazing how much i learned today even understand the basics of polymorphic relations now!

im going to try this out and let you know if it worked as soon as possible :)

kazehaya's avatar

Okey im stuck, im getting my name of the shops with this:

                $shops[] = Shop::whereHas('stores', function ($q) use ($store_id) {
                    $q->where('store_id', '=', $store_id);
                })->select('id', 'name')->get();

But how can i get the id of the pivot table instead of the id from the shop table?

bestmomo's avatar

This query is not to get the pivot id. You can get pivot fields with this kind of query :

$shop = Shop::find(1)

Now I have a shop, if I use the relation :

foreach ($shop->stores as $store)
{
    echo $store->pivot->id;
}

With the relation I can get a "pivot" attribute and get information on pivot table.

What do you want to achieve ?

kazehaya's avatar

Hey @bestmomo again thanks for your help :)

Im trying to change the input value of the checkboxes to match the id of the pivot table. Otherwise its value is not unique and that way i cant store the correct user_shop_id in the shop_store_user table.

What is happening now is that the value of the checkboxes is that of the shops table instead of the shop_store table. And that id i cant store in the shop_store_user table.

bestmomo's avatar

You should create new post for new question (because it becomes very long and is checked solved) so more people could answer ;)

For your question this could help you :

$shops = Shop::with('stores')->Has('stores')->get();
        
foreach ($shops as $shop) {
    echo $shop->name. '<br>';
    foreach ($shop->store as $store) {
        echo $store->name . '<br>';
        echo $store->pivot->id . '<br>';
    }
}

You get for each shop that have stores all stores with the pivot id.

Previous

Please or to participate in this conversation.