jeudi 16 juillet 2020

Laravel - return array of relationships in laravel model

I want to return a array in my services relationship. In my output, I want something like:

services: {
 true: {...},
 false: {...},
}

The relationship it self is working. If I just

return $this->belongsToMany('App\Service', 'product_services', 'product_id', 'service_id')->wherePivot('includes_it', 1)

Then it works fine. But I want to go deeper. I want that the services method returns an array with a false, and a true key.

Like here:

public function services()
    {
        return [
            "true" => $this->belongsToMany('App\Service', 'product_services', 'product_id', 'service_id')->wherePivot('includes_it', 1),
            "false" => $this->belongsToMany('App\Service', 'product_services', 'product_id', 'service_id')->wherePivot('includes_it', 0)
        ];
    }

But what I get is:

Error: Call to a member function addEagerConstraints() on array



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire