mardi 10 avril 2018

Eloquent query get relationship value for every result

I'm trying to get the value of a column from another table based on the ID of my current query, and apply this to all my results.

These are my relationships:

Model "AttributeOption"

public function attributes()
{
    return $this->hasMany('App\Attribute');
}

Model "Attributes"

public function attributeOptions()
{
    return $this->belongsTo('App\AttributeOption');
}

I have my result set as an array

$attributeResults = Attribute::whereIn('group_id', $attributes)->get()->toArray();

Heres a sample of my result from the query -

  0 => array:7 [▼
    "id" => 13
    "group_id" => 11
    "label" => "Food item"
    "type" => 1
    "value" => null
    "created_at" => "2018-04-08 20:49:26"
    "updated_at" => "2018-04-08 20:49:26"
  ]

Using the type column I want to look up the relationship using that value. I tried to run a test dd($attributeResults[0]->option)

Is there something I can do to my Eloquent query to get the relationship value during the query? I don't understand how you can do a relationship look up when its no longer an object, in this case, an array.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire