Laravel is one of the most popular PHP frameworks for web development, known for its simplicity and expressive syntax. In this tutorial, we will be building a basic CRUD (Create, Read, Update, Delete) application using Laravel 9. We will start by setting up the Laravel 9 environment, creating a database, and then building the CRUD application.
Setting up Laravel 9 Environment
To begin with, we need to install Laravel 9 on our system. We can do this using Composer, a PHP package manager. Open up a command prompt and run the following command:
composer create-project --prefer-dist laravel/laravel laravel-crud
This will create a new Laravel 9 project named "laravel-crud". We can navigate to this directory by running the following command:
cd laravel-crud
Creating a Database
Next, we need to create a database for our CRUD application. We can do this by using phpMyAdmin or any other database management tool. Once we have created the database, we need to update our Laravel 9 application's .env file with the database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_crud
DB_USERNAME=root
DB_PASSWORD=
Creating a Model and Migration
Now, we will create a model and migration for our CRUD application using the following command:
php artisan make:model Product -m
This will create a model named "Product" and a migration file for the "products" table in our database. We can update the migration file with the following code:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->float('price');
$table->timestamps();
});
}
The above code will create a "products" table with columns for "name", "description", "price", and "timestamps". We can run the migration using the following command:
php artisan migrate
Creating a Controller
Next, we will create a controller for our CRUD application using the following command:
php artisan make:controller ProductController --resource
This will create a controller named "ProductController" with resourceful methods for our CRUD operations. We can update the controller with the following code:
public function index()
{
$products = Product::latest()->paginate(5);
return view('products.index', compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'description' => 'required',
'price' => 'required'
]);
Product::create($request->all());
return redirect()->route('products.index')
->with('success', 'Product created successfully.');
}
public function show(Product $product)
{
return view('products.show', compact('product'));
}
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'description' => 'required',
'price' => 'required'
]);
$product->update($request->all());
return redirect()->route('products.index')
->with('success', 'Product updated successfully');
}
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success', 'Product deleted successfully');
}
In the above code, we have created resourceful methods for our CRUD operations. The index()
method fetches all products from the database and displays them in a paginated view. The create()
method displays a form to create a new product. The store()
method validates the request data and saves the new product to the database. The show()
method displays a single product. The edit()
method displays a form to edit an existing product. The update()
method updates the product in the database with the new data. The destroy()
method deletes a product from the database.