4
votes

I'm kinda stuck with this here and don't know how to move forward with this one.

I have two Models: user and child and they are in a Relationship.

( Keep in mind that this only illustrate the problem )

class Child extends Model{

    protected $primaryKey = 'id_child';

    public $appends = ['is_alive'];

    public function user(){

        return $this->belongsTo('Models\User','id_user');

    }

    public function getIsAliveAttribute(){

        if (!is_null($this->lifetime_updated_at))
            return (Carbon::parse($this->lifetime_updated_at)->addMinute($this->lifetime) >= Carbon::now());
        else
            return false;
    }
}

class User extends Model{

    protected $primaryKey = 'id_user';

    public $appends = ['is_alive'];

    public function childs(){

        return $this->hasMany('Models\Child','id_user');
}

public function getIsAliveAttribute(){

    if (!is_null($this->lifetime_updated_at))
        return (Carbon::parse($this->lifetime_updated_at)->addMinute($this->lifetime) >= Carbon::now());
    else
        return false;
}

}

Now I want to use Eager Loading in the Controller to retrieve my Childs data from User.

But my User Model Object comes from an MiddleWare in my Application. So I only have the User Model Object to use and I don't want to Query the User again using "with()".

$user = User::where('name','DoctorWho')->first();

return user->childs()->find(3);

What this operation returns:

    {
        "id_child": 3,
        "name": "JayZ",
        "last_name": "Etc",
        "lifetime": 1,
        "lifetime_updated_at": null,
        "created_at": "2017-05-29 21:40:02",
        "updated_at": "2017-05-29 21:40:02",
        "active": 1   
}

What I needed ( With Attribute Appended)

{
    "id_child": 3,
    "name": "JayZ",
    "last_name": "Etc",
    "lifetime": 1,
    "lifetime_updated_at": null,
    "created_at": "2017-05-29 21:40:02",
    "updated_at": "2017-05-29 21:40:02",
    "active": 1,
    "is_alive": true
  }

Is even possible to retrieve Child Data with Appended Attributes using Eager Loading ?

Notes: This user object come from an Middleware, I must use the User Model Object to get its child's with the appended attribute.

Thanks in Advance, LosLobos

2

2 Answers

0
votes

The thing is I was doing something wrong that is not related to Eloquent acessing the model like that does come with the appended attributes!

0
votes

What you're doing here is not eager loading. Your childs relationship is wrong. Should be return $this->hasMany('Models\Child', 'id_user'); based on the model and the other relationship defined.

Here are some ways you can access the child information. These by default should respect the $appends property and load the field.

$childId = 3;

$user = User::with('childs')
    ->where('name', 'DoctorWho')
    ->first();

return $user->childs()->where('id_child', $childId)->first();

// Or

$user = User::with(['childs' => function ($query) use ($childId) {
        $query->where('id_child', $childId);
    }])
    ->where('name', 'DoctorWho')
    ->first();

return $user->childs->first();

// Or

$child = Child::whereHas('user', function ($query) {
        $query->where('name', 'DoctorWho');
    })
    ->find(3);

return $child;

Edit :

If you already have the user model then you can do this.

$child = Child::where('id_user', $user->id_user)
    ->where('id_child', 3)
    ->first();

return $child;