Mastering Laravel 10 CRUD Operations: A Step-by-Step Guide with Code Examples

Published on : May 05,2023
Mastering Laravel 10 CRUD Operations: A Step-by-Step Guide with Code Examples

Laravel is a popular PHP web application framework that allows developers to build robust and scalable applications quickly. One of the essential features of any web application is CRUD (Create, Read, Update, Delete) functionality, which allows users to perform basic data operations.

In this blog post, we'll go through the process of implementing CRUD functionality in Laravel 10. Here's what we'll cover:

1. Setting up the Environment

Before we start, we need to set up the environment to run our Laravel application. We'll need to install PHP, Composer, and Laravel using the command line.

# Install PHP
sudo apt install php

# Install Composer
sudo apt install composer

# Install Laravel
composer create-project laravel/laravel my-app

 

2. Creating a Database and Table

Next, we'll create a MySQL database and table to store our data. We'll use Laravel's built-in migration tool to create a table and define its columns.

# Create migration
php artisan make:migration create_users_table

# Define table structure
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}





# Run migration
php artisan migrate

 

3. Creating the Model

The model represents the data and interacts with the database. We'll create a model class that defines the table and its relationships.

# Create model 
php artisan make:model User
# Define table and relationships
	class User extends Model
 	{   
 		protected $table = 'users';
 		protected $fillable = ['name', 'email', 'password'];
 	}

 

4. Creating the Controller

The controller handles user requests and calls the appropriate model method to perform CRUD operations. We'll create a controller class with methods for each CRUD operation.

# Create controller
php artisan make:controller UserController
# Define CRUD methods
class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
        return view('users.index', compact('users'));
    }

    public function create()
    {
        return view('users.create');
    }

    public function store(Request $request)
    {
        User::create($request->all());
        return redirect()->route('users.index');
    }

    public function edit(User $user)
    {
        return view('users.edit', compact('user'));
    }

    public function update(Request $request, User $user)
    {
        $user->update($request->all());
        return redirect()->route('users.index');
    }

    public function destroy(User $user)
    {
        $user->delete();
        return redirect()->route('users.index');
    }
}

 

5. Creating the Views

The views are the user interface for our application. We'll create four views for each CRUD operation: create, read, update, and delete.

# Create views
resources/views/users/index.blade.php
resources/views/users/create.blade.php
resources/views/users/edit.blade.php

 

6. Routing

We'll create routes in Laravel to handle user requests and direct them to the appropriate controller method.

# Define routes
Route::get('/', function () {
    return redirect()->route('users.index');
});

Route::resource('users', UserController::class);

 

7. Testing

Finally, we'll test our application by performing CRUD operations on the database using the user interface.

# Test CRUD operations in web browser
http://localhost:8000/users
http://localhost:8000/users/create
http://localhost:8000/users/1/edit

 

By following these steps, we can implement CRUD functionality in Laravel 10 and create a fully functional web application. Laravel provides a powerful framework for building web applications quickly and easily, and with CRUD functionality, we can create applications that allow users to interact with data in meaningful ways.

Overall, implementing CRUD functionality in Laravel 10 is a straightforward process that any developer can learn with practice. With the right tools and techniques, you can build robust and scalable applications that meet the needs of your users.

Categories : Laravel

Tags : PHP Laravel 10 CRUD operations code examples web development CRUD

Abhay Dudhatra
Abhay Dudhatra
I am a full-stack developer who is passionate about creating innovative solutions that solve real-world problems. With expertise in technologies such as PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap, I love to share my knowledge and help others in the industry through writing tutorials and providing tips. Consistency and hard work are my mantras, and I constantly strive to improve my skills and stay up-to-date with the latest advancements in the field. As the owner of Open Code Solution, I am committed to providing high-quality services to my clients and helping them achieve their business objectives.


0 Comments

Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles