Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
access to parent model from withDefault closure (#23334)
Sometimes, you may wish to return default value of relations using advanced logic and condition base of parent values This already support but not work for lazy eager loading for example ```php // Message model public function user() { return $this->belongsTo(User::class)->withDefault(function ($user) { $user->name = $this->getAttribute('username'); $user->email = $this->getAttribute('email'); return $user; }); } // single record. $message = Message::first(); $message->user->name; // return username from message model as except. // but i we try access to user info use lazy eager loading $messages = Message::with('user')->get(); $messages->first()->user->name; // return null ``` This PR support pass parent model to closure to make sure the parent model available in all cases. ```php public function user() { return $this->belongsTo(User::class)->withDefault(function ($user, $parent) { $user->name = $parent->getAttribute('username'); $user->email = $parent->getAttribute('email'); return $user; }); } ``` thanks
- Loading branch information