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:
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
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
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'];
}
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');
}
}
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
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);
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