Laravel is a recognized PHP framework renowned for its complex syntax and robust capabilities. Creating, reading, updating, and deleting content are referred to as CRUD operations, and they are one of the most common activities in web development.
Required conditions
Before we get started, check that the necessary software is completely installed on your computer:
- Composer
- PHP
- Laravel Command Line Interface
To install Laravel Command Line Interface (CLI) with Composer, you can execute the following command:
Composer global require Laravel/installer
Step 1: Creating a Laravel Project
First things first, let’s produce a brand new Laravel project. To execute the following command, open your terminal and type it in:
laravel new crud-demo
Step 2: Setup Database Config
Going ahead, you have to set up a database connection in the.env file. The DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD settings should be adjusted according to your database’s configuration.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Step 3: Establish a Migration
The migrations feature in Laravel allows you to define your database schema using PHP code. Execute the command provided below to generate a migration for the resource you want. As an illustration, let’s make a table for users’ posts:
php artisan make:migration create_posts_table
In the directory designated for migrations, a migration file will be created due to this action. Launch this file, then create the schema for your table under the up procedure. Just one example:
In order to create the table in your database, you should run the migration:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
In order to create the table in your database, you should run the migration:
php artisan migrate
Step 4: Create Model
Within Laravel, models are used to represent your data and provide you with the ability to communicate with your database. Make a model by utilizing the command that is shown below:
php artisan make:model Post
A Post model will be created in the app/Models directory as a result of this action.
Step 5: Create Routes
Use the routes/web.php file to define the routes that will be used for your CRUD activities. Just one example:
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::post('/posts', [PostController::class, 'store']);
Route::put('/posts/{post}', [PostController::class, 'update']);
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
Step 6: Create Controller
To manage your CRUD activities, you should create a controller. To generate a controller, execute the following command here:
php artisan make:controller PostController
A PostController will be created in the app/Http/Controllers directory as a result of this action.
Step 7: Implement CRUD Operations
Open the PostController and implement the CRUD operations using the Post model. Here’s an example implementation:
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return Post::all();
}
public function show(Post $post)
{
return $post;
}
public function store(Request $request)
{
return Post::create($request->all());
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return $post;
}
public function destroy(Post $post)
{
$post->delete();
return response()->json(null, 204);
}
}
Step 8: Test Your Endpoints
Finally, test your CRUD endpoints using tools like Postman or creating views and forms in your Laravel application.
You’ve successfully implemented CRUD operations in Laravel. This foundational skill will help you build dynamic web applications with Laravel.
Wrapping It Up
By following these steps, you can successfully manage data in your Laravel application, allowing users to easily create, read, update, and delete entries.
Remember that CRUD operations form the foundation of most web applications, and knowing them is essential for effective Laravel programming. Furthermore, as you go with Laravel, you’ll learn more sophisticated approaches and features that will help you improve your development skills.
Helpful Read – Best Open Source Laravel Projects
Comments