Laravel Get method return value for each related model

As @lagbox said in the comment box, you need an accessor.

Location model :

public function getStatusAttribute()
{
    $result = [];

    if($this->deleted_at)
    {
        $result['color'] = 'danger';
        $result['value'] = 'Inactive';
    }
    elseif($this->is_onboarding_completed)
    {
        $result['color'] = 'success';
        $result['value'] = 'Active';
    }
    else
    {
        $result['color'] = 'warning';
        $result['value'] = 'Onboarding';
    }

    return $result;
}

Controller :

public function show($id)
{
    $client = Client::findOrFail($id);        
    $locations = $client->locations()->withTrashed()->get();

    foreach($locations as $location)
    {
       dd($location->status);
    }

    ...
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top