jeudi 26 avril 2018

Laravel 5.6 Eager Loading specific column returns nothing

I have two classes, Product and ProductFormat. The relationship is defined properly, my Product hasMany ProductFormat.

public function formats()
{
    return $this->hasMany(ProductFormat::class);
}

When I'm trying to eager load the relationship with specific columns, as followed in the documentation (https://laravel.com/docs/5.6/eloquent-relationships#eager-loading), it's not working as expected.

For example, when I do the following:

Product::with('formats:id,upc')->get();

I get my products, with empty formats everywhere.

{
    id: 1,
    formats: [ ]
}

However, if I do the following:

Product::with('formats')->get();

I get the expected formats, but it has too many non needed columns.

{
    id: 1,
    formats: [
        {
            id: 1,
            upc: "101862422191",
            weight: 8.46,
            weight_unit: "kg"
        }
   ]
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2vUKKNY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire