Deep Dive into Model and Necessary Attributes
Hello Artisans, In this chapter we'll discuss the model and all the necessary attributes of the model which can reduce a lot of manual work. A model represents a single table in your database and provides an interface for interacting with that table's data. So if you already complete the Second chapter/section you're good to go, if not my recommendation would be please complete the second one.
- $table
- $primaryKey
- $fillable
- $guarded
- $casts
- Accessors
- Mutators
Now we'll see the usage of these attributes and how each of the attributes can reduce our work.
This attribute defines the name of the database table that the model represents. By default, Laravel will use the pluralized name of the model class as the table name. But in a case where you need to use a table that goes totally different or doesn't contain the pluralized word then this attribute will take care of it. Suppose your model name is Attendance and according to Laravel it'll by default expect the ‘attendances’ table. But if your table name is ‘employee_attendances’ then what you'll do. No worry, see the below example.
Now your Attendance model is representing the ‘employee_attendances’.
The primary key is a column in a database table that uniquely identifies each row in that table. By default, Laravel assumes that the primary key column is named id, but we can override this by setting the $primaryKey attribute on your model. So if we take the previous model “Attendance” as an example. Whenever we call it expects to use "id" as a primary key but we want to use "attendance_id" as a primary key. And that's why $primaryKey is there. Let's see the below example.
Note that if you use “attendance_id” as a primary key you've to declare while using foreignId as well. See the below example
The fillable property on a model is used to specify which attributes can be mass assigned using the create() method or the fill() or the update() method. Mass assignment is the process of setting multiple attributes on a model at once, typically using an array or a request object.
Here's an example of defining the fillable property on a User model:
and the in the controller we can use it like these
And it'll save the data in the database for you and also reduce some extra words by specifying each of the attributes like the following
and this can be huge based on your data input.
The $guarded property on a model is used to specify which attributes should not be mass-assigned using the create() method or the fill() method. Mass assignment is the process of setting multiple attributes on a model at once, typically using an array or a request object. i.e. we can say that it's opposed to $fillable property. That means if a property is define as guarded property, we can not set it by using create(), update() or fill() method.
Here's an example of defining the $guarded property on a User model:
The $casts property on a model is used to specify which attributes should be automatically cast to certain data types when retrieved from the database or saved to the database. This can be useful for automatically transforming data between PHP and database-specific types, such as JSON or array values. Suppose we want to store a JSON value in our field. What we do is
because we cannot save the array value to the database and without doing encoding if we try to save we can see the below error like
“Array to string conversion”. And when we want to retrieve we've to do below
But hang on, we ca don't need to do this for all staff, we've the $casts attributes. Let's see how we can easily handle this situation with $casts attribute.
Now we don't need any json_encode() or json_decode(). Let's look at the below code snippet.
and while retrieving
These are methods on the model that are used to retrieve a value from an attribute before it is returned to the calling code. Accessors are named in a specific format: get[AttributeName]Attribute. For example, if you have an attribute named “first_name” and “last_name”, you can define an accessor method like the below:
Now every time when we need to get the full name of a user, we need to call this way
We don't need to contact every time like below
These are methods on the model that are used to set the value of an attribute before it is saved to the database. Mutators are named in a specific format: set[AttributeName]Attribute. For example, if you have an attribute named name, you can define a mutator method like this:
We'll see the detail of each and every attribute which I'll discuss in this chapter. That's it for today. See you in my fourth chapter. Happy coding 🙂 .
Thank you for being so supportive!
1 Comments
:)
ReplyDeleteCAN FEEDBACK
Emoji