0 votes
in Laravel by
What are accessors and mutators?

1 Answer

0 votes
by

Accessors are a way to retrieve data from eloquent after doing some operation on the retrieved fields from the database. For example, if we need to combine the first and last names of users but we have two fields in the database, but we want whenever we fetch data from eloquent queries these names need to be combined.

What the above code will do is it will give another attribute(full_name) in the collection of the model, so if we need the combined name we can call it like this: `$user->full_name`. Mutators are a way to do some operations on a particular field before saving it to the database.

Related questions

0 votes
asked Oct 26, 2023 in Laravel by rajeshsharma
+1 vote
asked Jul 12, 2021 in Laravel by sharadyadav1986
...