Models in Laravel: Understanding $guarded, $fillable, Relationships and Code Examples
Models
Models in Laravel are classes that represent the data and the business logic of the application. In Laravel, models are typically used to interact with the database, retrieve, store and manipulate data.
You can create models in Laravel by running the following command in your project directory.
php artisan make:model model-name
or you can run this command to create model, controller and migration in one go.
php artisan make:model model-name -c -m
-c creates the controller for you model and -m creates the migrations.
Do you know by default in Laravel the Laravel model name is the Singular name for the table name
Eg. your table name: products - then create your model name as Product
It'll automatically map the model to the table name and you don't have to specify the table name in model
Here's an example code of a model in Laravel:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { // }
The above code creates a model named Product that extends the Model class provided by Laravel's Eloquent ORM. With this basic structure, you can start interacting with the database and retrieving data. You can also specify the table name by overriding the protected $table property of the model.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $table = "MyProduct"; // table name - my_product }
The $guarded property in Laravel
In Laravel, the $guarded property is used in the model to specify which attributes cannot be mass-assigned. This property is an array of column names that cannot be updated using the update method. The $guarded property is the opposite of the $fillable property.
Here's an example of how to use the $guarded property in a Laravel model:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $guarded = [ 'id', 'created_at', 'updated_at', ]; }
In the example above, the $guarded property is set to an array of three column names, id, created_at, and updated_at. These columns cannot be updated using the update method in the model.
If neither the $fillable nor the $guarded properties are defined in the model, all attributes are mass-assignable by default.
It's important to use the $fillable or the $guarded property in your models to prevent mass-assignment vulnerabilities in your application. By specifying the attributes that can or cannot be mass-assigned, you can ensure that only the desired attributes are updated when using the update method.
The $fillable property in Laravel
The $fillable property in Laravel models is used to specify the attributes that are mass-assignable. This property is an array of column names that can be updated using the update method. For example:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $fillable = [ 'name', 'price', 'description', ]; }
In the example above, the $fillable property is set to an array of three column names, name, price, and description. These columns can be updated using the update method in the model.
Relationships
In Laravel, models can also define relationships with other models, such as one-to-one, one-to-many, and many-to-many relationships. This allows you to easily retrieve related data.
For example, consider a Product model and a Category model. The Product model has a one-to-one relationship with the Category model, meaning each product belongs to a single category. To define this relationship in the Product model, you can use the belongsTo method:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $fillable = [ 'name', 'price', 'description', ]; public function category() { return $this->belongsTo(Category::class); } }
In the example above, the category method returns a one-to-one relationship with the Category model. This allows you to easily retrieve the category for a product, for example:
$product = Product::find(1); $category = $product->category;
In conclusion, models in Laravel are essential components that represent the data and business logic of the application. The $fillable property allows you to specify which attributes can be mass-assigned and the relationships allow you to easily retrieve related data.