Javatpoint Logo
Javatpoint Logo

Laravel database

In this topic, we will learn how to work with a database and run the queries in an application. There are many ways to work with a database, and the first way is to use the raw sql queries in an application, and the second way is to use the eloquent model that also provides the interaction with the database.

In this topic, we will learn about the raw sql queries through which we can insert, retrieve, update, and delete the data.

Inserting the data

Now, we will insert the data into a database. First, we create the route in a web.php file.

In the above code, the '/insert' is the url which inserts the data in a database. In function closure, DB is the class that implements the insert() function. The insert() function contains two parameters, i.e., first parameter is the insert command that contains the name of the columns and the second parameter represents the array containing the values of columns.

Output

When we run the url "http://localhost/firstproject/public/insert", then the data gets inserted in a database.

Laravel database

In the above output, the highlighted area shows that the data has been successfully inserted in a database.

Reading the data

Now, we will look at how to retrieve the data from the database. Let's first create the route in web.php file.

In the above code, the '/select' is the url that retrieves the data from the database. The DB is the class that implements the select() method, and the select() method contains two parameters. The first parameter is the select command, and the second parameter is the array that represents the value of the id.

Output

When we run the url "http://localhost/firstproject/public/select", then the output would be:

Laravel database

Updating the data

Now, we are going to update the data in the database. Let's first create the route in the web.php file.

In the above code, the '/update' is the url that updates the data in a database. The DB is the class that implements the update() method that contains two parameters. The first parameter contains the update command, and the second parameter is the array that represents the value of id. It updates the title with "software tester" where id is equal to 1. Let's see the output of the above code:

Laravel database

The above output shows that '/update' returns the value 1; it means that the data is updated successfully.

Laravel database

In the above output, the highlighted area shows that the title is changed from the software developer to the software tester.

Deleting the data

Now, we will see how to delete the data from the database. Let's first create the route in the web.php file.


Laravel database

The above output shows that '/delete' url returns the value 1; it means that the record has been deleted successfully.

Laravel database

We know that only one record with id=2 is available in the posts table. The above output shows that the record of id =2 is deleted.


Next TopicLaravel Eloquent




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA