Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Show the code calling relations and the queries that are pulling data from other table

Last updated 2 years ago.
0

The following code (including authentication before the route)

$options = Product::find(18)->options;

Produces the following data dump from the query log

array (size=3)
  0 => 
    array (size=3)
      'query' => string 'select * from `users` where `id` = ? limit 1' (length=44)
      'bindings' => 
        array (size=1)
          0 => string '1' (length=1)
      'time' => float 0.38
  1 => 
    array (size=3)
      'query' => string 'select * from `products` where `id` = ? limit 1' (length=68)
      'bindings' => 
        array (size=1)
          0 => int 18
      'time' => float 0.29
  2 => 
    array (size=3)
      'query' => string 'select `options`.*, `product_option`.`product_id` as `pivot_product_id`, `product_option`.`option_id` as `pivot_option_id` from `options` inner join `product_option` on `options`.`id` = `product_option`.`option_id` where `product_option`.`product_id` = ?' (length=323)
      'bindings' => 
        array (size=1)
          0 => string '18' (length=2)
      'time' => float 0.31

If I then access the countries relationship like so:

foreach ($options as $option) {
    $option->pivot->countries;
}

The following query is appended to the query log

  3 => 
    array (size=3)
      'query' => string 'select `countries`.*, `product_option_country`.`product_option_id` as `pivot_product_option_id`, `product_option_country`.`country_id` as `pivot_country_id` from `countries` inner join `product_option_country` on `countries`.`id` = `product_option_country`.`country_id` where `product_option_country`.`product_option_id` is null' (length=402)
      'bindings' => 
        array (size=0)
          empty
      'time' => float 0.28
Last updated 2 years ago.
0

And what's your product_option_country table?

You want to query pivot table product_option_country for the price of given option-country set?

Last updated 2 years ago.
0

product_option_country is a second pivot table, between the ProductOptionPivot model and the Country model. On this pivot table, there will be a price, which will be accessed through pivot->price;

Last updated 2 years ago.
0

Solved it - had to do ->withPivot('id') on the Product->belongsToMany('Option') relationship so that the ID was pulled through of the pivot table, otherwise the ID of the pivot table is null (this can be seen on the last entry of the query logs I posted above) and you obviously can't join null values!

Last updated 2 years ago.
0

@sixteenstudio

Could you please have a look at my thread: http://laravel.io/forum/05-23-2014-belongstomany-relationsship-on-custom-pivot-model

I have a similar problem but I just don't get it...

Many thanks!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.