mardi 18 juin 2019

Load Laravel eloquent attribute by string

I am storing some strings in my database, and I want to be able to get model attributes using them.

Basically, I can call a users booking like this

$user->bookings->date

in my controller and it works, and i'm storing a string in my database like this

'user->bookings->date'

How can I use this string from the database to get the attribute? I'm hoping to be able to do something like

$user = User::findOrFail(1);
$string = 'user->bookings->date'; //This is fetched from the db normally
$booking_date = ${$string}; //Instead of writing $user->bookings->date

Is this possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire