Laravel Models and Eloquent ORM for Beginners


Laravel's Eloquent ORM (Object-Relational Mapping) is a powerful feature that simplifies database interaction and makes it more accessible to developers. In this guide, we will explore the basics of Laravel models and Eloquent ORM, providing a foundation for beginners to work with databases in their web applications.


What are Models?


In Laravel, a model represents a specific table in your application's database. Models allow you to interact with your database records using object-oriented syntax. They serve as an intermediary between your application and the database, enabling you to perform common database operations like creating, reading, updating, and deleting records.


Creating a Model


Laravel's Artisan command-line tool makes it easy to generate models. To create a new model, open your terminal and run the following command:


        
php artisan make:model MyModel

This command generates a model file named `MyModel.php` in the `app` directory. You can use this model to interact with a corresponding database table named `my_models` by default.


Defining Model Properties


In your model, you can define properties that correspond to the columns in your database table. This allows you to access and manipulate the data using object notation. For example, if your database table has a `name` column, you can define a `name` property in your model:


        
protected $fillable = ['name'];

The `$fillable` property is an array that specifies which model attributes are mass-assignable, meaning they can be set directly using methods like `create()` and `update()`. It helps protect your application from mass-assignment vulnerabilities.


Retrieving Records


With a model in place, you can easily retrieve records from the database. For example, to fetch all records from the `my_models` table, you can use the following code:


        
$records = MyModel::all();

This code retrieves all records from the `my_models` table and stores them in the `$records` variable as a collection. You can then iterate through the collection to work with individual records.


Creating and Updating Records


Creating new records and updating existing ones is straightforward with Eloquent. To create a new record, you can use the `create()` method:


        
MyModel::create(['name' => 'New Record']);

Updating a record can be done by retrieving it, modifying its properties, and then calling the `save()` method:


        
$record = MyModel::find(1);
$record->name = 'Updated Record';
$record->save();

Deleting Records


To delete a record, you can use the `delete()` method on a retrieved model instance:


        
$record = MyModel::find(1);
$record->delete();

This code deletes the record with an ID of 1 from the `my_models` table.


Conclusion


Laravel models and Eloquent ORM provide a user-friendly and efficient way to interact with databases in your web applications. Understanding the basics of models and Eloquent allows you to perform common database operations with ease. As you progress in your Laravel journey, you can explore more advanced topics like relationships, query scopes, and validation for enhanced database interactions.


For further learning, consult the official Laravel documentation and explore practical tutorials and examples in Laravel web development.