Javatpoint Logo
Javatpoint Logo

Laravel Eloquent

In this topic, we will learn about the eloquent model that allows the interaction with a database. Each database table has its corresponding model that provides the interaction with a database. The model allows you to query the data in your tables.

Models are created in the app directory. You can also place the model anywhere, which can be loaded automatically according to the composer.json file.

We can use the following command to create the model:

php artisan make:model Post

We can also generate the model by using database migration:

php artisan make:model Post -m

or

php artisan make:model Post -migration

Steps to create the model

  • When we enter the above command in a Git bash window:
Laravel Eloquent

The above window shows that the model with the name 'Post' has been created successfully.

  • The model (Post) is created in the app directory.
Laravel Eloquent

Structure of the model

The structure of the model class that we created above is shown below:

The above code shows that the class Post extends the Illuminate\Database\Eloquent\Model.

Table names

In laravel eloquent, we do not need to specify the table name to be used for the Post model. The plural name of the class will be considered as the table name unless we specify the name of the table explicitly. For example, in the above code, the name of the class is Post that works on the table posts. You can also specify the custom table by using the $table attribute of the model class shown in the below code:

In the above code, the $table attribute specifies that the Post class is using the posts table.

Primary keys

The eloquent model considers that each table has a primary key named 'id'. We can override this convention by providing a different name to the $primarykey attribute.

By default, in eloquent, the primary key is an auto-incremented integer value. If we want to provide the non-incrementing value to the primary key, then we have to set the $incrementing attribute 'false'.

public $incrementing = false;

If we want to provide the non-integer value to the primary key, then we have to provide a different value to the $keyType attribute.

protected $keyType = 'string';

In the above, we are assigning string type to the primary key.

Reading data

Now, we will look at how to retrieve the data from the database. Let's understand through an example.

  • First, we need to add the two attributes in a model class.

Post.php

  • We add the route which is used to retrieve the data from the database.

web.php

In the above code, we use the all() method that retrieves all the records from the database, and then we apply the foreach loop to retrieve the body name of all the rows available in the database, which is shown below.

In the below screenshot, we can see that two records are available in the posts table.

Laravel Eloquent

Output

When we run the url, localhost/firstproject/public/read, the output would be:

Laravel Eloquent

If we want to retrieve the particular record from the database, then we use the find() method.

Output

Run the url, localhost/firstproject/public/find to view the output of the above code.

Laravel Eloquent

Reading data with constraints

  • To retrieve a single row, we use the first() method as shown below:

Output

Laravel Eloquent
  • If we do not need to retrieve the entire row, then we can use the value() method to retrieve the value of a column directly.

Output

Laravel Eloquent

Inserting data

Now, we will see how to insert the data in a database. Let's look at an example given below:

Output

Run the url, localhost/firstproject/public/insert in a web browser. After executing the url, open the phpmyadmin.

Laravel Eloquent

The above output shows that the data has been inserted successfully.

Updating Data with the save() method

We can also update the records by using the save() method. Let's understand through an example.

Output

Laravel Eloquent

The above screen shows the database table, which is before the execution of the above code.

When we execute the above code, the data gets updated shown in the below screen.

Laravel Eloquent

Mass Assignment

To provide the mass assignment, we need to use the create() method and also provides the $fillable property in a model class.

Let's understand through an example.

  • First, create the route, and we added the create() method in the closure function. The create() method is basically adding a new record, and the values are provided through its parameter.First, create the route, and we added the create() method in the closure function. The create() method is basically adding a new record, and the values are provided through its parameter.
  • To provide the mass assignment, we need to add the $fillable attribute in a model class as shown in the below code.

Output

Run the url, localhost/firstproject/public/create to run the above code.

Laravel Eloquent

Now, look at the database.

Laravel Eloquent

The above highlighted-area shows that the new record has been created successfully.

Updating data with Eloquent

Now, we will see how to update the data by using eloquent. Let's understand through an example.

  • First, create the route.

In the above code, we use the update() method of the model class. We are updating the record which is having id equal to 1.

Output

Laravel Eloquent
Laravel Eloquent

Deleting data

Now, we will see how to delete the data by using Eloquent. We directly implement the delete() method available in the eloquent model class.

There are different ways of deleting the data.

  • The first way is to use the find() and delete() method.

Output

Laravel Eloquent
Laravel Eloquent
  • The second way is to use the destroy() method.

Output

Laravel Eloquent
Laravel Eloquent

If we want to destroy more than one row,

The above code is destroying the records having id, 3 and 4.

Output

Laravel Eloquent
Laravel Eloquent
  • The third way is to use the query.

Output

Laravel Eloquent
Laravel Eloquent

Soft Deleting/Trashing

There is also another way of deleting the records is soft deleting. When models are soft deleted, it means that records are not actually removed from the database. In soft deleting, records are not permanently deleted; they are stored in the trash space.

Let's understand through an example of how soft deleting is done.

  • First, we need to set the deleted_at attribute to the model class.
  • Now, make migration to add a deleted_at column in the posts table.
Laravel Eloquent
  • As we have created the migration with a name add_column_deleted_at, its structure is given below:

The laravel contains the helper method known as softDeletes(), which we have used in the above code. The softDeletes() method is used to create the column.

  • Now, run the command php artisan migrate.
Laravel Eloquent
  • At the end, add the route in web.php file to run the soft-deletions.

In the above code, we are soft-deleting the record which is having id '1'.

Output

Laravel Eloquent
Laravel Eloquent

In the above screen, deleted_at column shows the time at which this record has been soft-deleted. If this column contains the null value, it means that this record is not soft-deleted.

Retrieving deleted/trashed data

To retrieve the deleted data, we use the withTrashed() method. Let's understand this through an example.

In the above, we are retrieving the record which is trashed or soft deleted.

Output

Laravel Eloquent

Restoring deleted/trashed data

In the previous topic, we see how to retrieve the data from the soft-deleted model. Now, we will see how to restore the data from the trash space to the original place. Let's understand this through an example.

In the above code, we are restoring the data which is trashed by using the restore() function.

Output

Laravel Eloquent
Laravel Eloquent

The above screen shows that the value of the deleted_at column is NULL which means that the record is restored in a database.

Deleting records permanently

Sometimes we need to remove the data permanently. To delete the soft-deleted model permanently, we use the forceDelete() method. Let's understand this through an example.

In the above code, we are deleting the data which are trashed.

Before the execution of the above code, the trashed record is having an id equal to 1 shown in the below screenshot.

Laravel Eloquent

When we run the above code, the trashed record will get deleted, and the table would like, as shown below:

Laravel Eloquent




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA