Kevin Bradshaw
Kevin Bradshaw

Reputation: 6417

Laravel Eloquent ORM eager loading. Relation incorrectly returned as null

I have an Eloquent ORM relationship defined as follows:

ProductConfiguration:

    public function product()
    {
        return $this->belongsTo('Excel\Products\Product');
    }

    public function currency()
    {
        return $this->belongsTo('Excel\Currencies\Currency');
    }

Product

    public function productConfigurations()
    {
        return $this->hasMany('Excel\Products\ProductConfiguration');
    }
    public function productType()
    {
        return $this->belongsTo('Excel\Products\ProductType');
    }

I expect that if I do the following that I will load all product configurations of a specified product type, with the related products, nested product type details and the product configuration currency

$results = ProductConfiguration::with(
            array(
                'product' => function($query) use ($product_type) {
                        $query->where('product_type_id' , $product_type);
                    },
                'product.productType',
                'currency'
            )
        )


            ->get();

however the returned collection has 'product' set to NULL. the Currency Relationship is there, but the product relationship is not. I can see the outputted SQL queries and the query that selects the products retrieves the correct products if I paste it directly into my sql editor

select * from `products` 
where `products`.`id` in ('12', '13') 
and `product_type_id` = '1'

Am I correct to think that the results from this query should be included in my collection, or is there some obvious flaw in my thinking?

Upvotes: 0

Views: 988

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

I think you don't want to achieve that. Now what you get is getting all ProductConfiguration with products that are only of certain_type.

So in case you have some configuration that has other type for product you will get null because you limited results from product to only the one that has certain product type.

I might be wrong, but you probably wanted to get those ProductConfiguration that belongs to Product that is type of certain_type. In this case you should use whereHas:

$results = ProductConfiguration::
          with('product', 'product.productType', 'currency')->
          whereHas('product', function($q) use ($product_type)
          {
            $q->where('product_type_id', '=', $product_type);

          })->get();

Upvotes: 1

Jeff
Jeff

Reputation: 2068

I hate to post this as an answer but since i don't have enough rep to comment so try this first:

$results = ProductConfiguration::with('product')->get();

dd($results->toArray());

See what you get, if you get some data, try this

$results = ProductConfiguartion::with(array('products' => function($query){
    $query->where('product_type_id' , $product_type);
})->get();

dd($results);

See what you get, if you get null: your $product_type variable may be something you didnt expect, so try dd($product_type) to make sure its what your expecting.

Upvotes: 1

Related Questions