CRUD Operations with Laravel Database
CRUD operations (Create, Read, Update, Delete) are the most common operations performed on a database. Laravel is a PHP web application framework that provides an elegant syntax to perform these operations on a database. In this article, we'll explore the Laravel database and how to perform CRUD operations.
Create Operation
The create operation adds a new record to the database.
Syntax
DB::table('tableName')->insert([
'column1' => 'value1',
'column2' => 'value2',
...
]);
Example
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'johndoe@example.com',
'password' => Hash::make('password')
]);
Output
A new record will be added to the 'users' table with the provided values.
Explanation
The create operation adds a new record to the database. In Laravel, it can be performed using the insert
method of the query builder.
Use
The create operation is used to add new records to the database.
Important Points
- The
insert
method accepts an array of column-value pairs. - The
Hash::make
method is used to hash the password before storing it in the database.
Read Operation
The read operation retrieves records from the database.
Syntax
DB::table('tableName')->get();
Example
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name;
}
Output
The code above will output the name of each user in the 'users' table.
Explanation
The read operation retrieves records from the database. In Laravel, it can be performed using the get
method of the query builder. The get
method returns a collection of objects, each representing a record in the table.
Use
The read operation is used to retrieve records from the database.
Important Points
- The
get
method returns a collection of objects. - The collection can be looped through to access each record.
Update Operation
The update operation modifies records in the database.
Syntax
DB::table('tableName')
->where('conditionColumn', 'conditionValue')
->update([
'column1' => 'value1',
'column2' => 'value2',
...
]);
Example
DB::table('users')
->where('id', 1)
->update(['name' => 'Jane Doe']);
Output
The name of the user with id
1 will be updated to 'Jane Doe'.
Explanation
The update operation modifies records in the database. In Laravel, it can be performed using the update
method of the query builder. The update
method accepts an array of column-value pairs to update the specified columns.
Use
The update operation is used to modify records in the database.
Important Points
- The
update
method accepts an array of column-value pairs. - The
where
method is used to specify the condition for the update.
Delete Operation
The delete operation removes records from the database.
Syntax
DB::table('tableName')->where('column', 'value')->delete();
Example
DB::table('users')->where('id', 1)->delete();
Output
The user with id
1 will be removed from the 'users' table.
Explanation
The delete operation removes records from the database. In Laravel, it can be performed using the delete
method of the query builder. The where
method is used to specify the condition for the delete operation.
Use
The delete operation is used to remove records from the database.
Important Points
- The
delete
method removes all records that match the specified condition. - The
where
method is used to specify the condition for the delete operation.
Summary
In this article, we explored the Laravel database and how to perform CRUD operations. Laravel provides an elegant syntax for performing database operations, which makes web development easier and faster. By mastering CRUD operations, you'll be able to create powerful and robust web applications.