I have a base User
model object. Each user can be either a staff member, parent or student. Each account type also has an associated model.
For example:
class User extends Model {
public function details()
{
switch($this->account_type) {
case 'staff': return $this->hasOne(Staff::class);
case 'student': return $this->hasOne(Student::class);
case 'parent': return $this->hasOne(ParentUser::class);
}
return null;
}
If I run:
echo $user->details->staff_code;
Then it outputs CRS
for example. However, if I run it with object_get()
:
object_get($user, 'details.staff_code');
Then it outputs null
.
I figured because in the object_get()
method, it has this line:
if (! is_object($object) || ! isset($object->{$segment}))
I assume because the details
property is a magic/dynamic laravel model property, isset()
doesn't work on it.
Whats the best way to handle this? Is it safe to edit the object_get()
method and replace the isset()
segment with something more robust? I don't really want to edit source code in the Vendor directory.
Should I create my own object_get
helper function, and if so, where should I place it / autoload it?
Is there another method of achieving the result I want that I'm unaware of?
Any help is appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire