Laravel 8 Eloquent CRUD Web Application Tutorial
Tags: MySQL, Laravel Eloquent, Laravel, PHP, Crud

In the previous tutorial, we have set up an admin panel with Laravel 8. In this tutorial, we’ll be learning how to create a CRUD app with Laravel 8 to create, update, and delete records from a MySQL database.

Instead of writing raw SQL queries to do CRUD, we will be using Laravel 8 Eloquent to program the CRUD and manage the persistent data. Laravel Eloquent is an Object Relational Mapper (ORM) which helps in communication with the database. Each database table will have a corresponding “Model”.

Step 1: Run artisan

Firstly, run the command below to create a Product model and controller using Artisan Tool.

php artisan make:model -a

With the -a arguments, Laravel will automatically create model, controller and migration file.

Step 2: Setup Database

Next, update the database connection in environment variable file .env in the root folder as below.

Navigate to the database/migrations directory, open the new product migration file. In Laravel, the migration file is used to managing the table schema. You can create a table column and data type using PHP rather than SQL. Migration provides version controlling, so each table/model can have many migration files that run chronologically to update the changes.

In each migration file, there are two methods up() and down(). up() method is used to add new tables, columns or indexes to your database. The down() method is used to drop the table.

Update the migration file as below:

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->longText('description');

$table->decimal('price', 5);

$table->timestamps();



});

}

Then, run the migrate command to execute the migration.

php artisan migrate

What if you want to undo some of your migrations? You can use the rollback command to retract the last migration that has been executed.

php artisan migrate:rollback

Or, reset everything

php artisan migrate:reset

Step 3: Setup Controller

If you run the step one command, a Product Controller will be created automatically under app/Http directory. Open the ProductController.php file and update the code as below:

<?php namespace App\Http\Controllers; use App\Models\Product;

use Illuminate\Http\Request; class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

//

$products = Product::all();

return view('product.index', compact('products'));

} /**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('product.create');

} /**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'title' => 'required',

'description' => 'required',

'price' => 'required',

]); Product::create($request->all()); return redirect()->route('product.index')

->with('success', 'Product created successfully.');

} /**

* Display the specified resource.

*

* @param \App\Models\Product $product

* @return \Illuminate\Http\Response

*/

public function show(Product $product)

{

return view('product.show', compact('product'));

} /**

* Show the form for editing the specified resource.

*

* @param \App\Models\Product $product

* @return \Illuminate\Http\Response

*/

public function edit(Product $product)

{

return view('product.edit', compact('product'));

} /**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Models\Product $product

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Product $product)

{

$request->validate([

'title' => 'required',

'description' => 'required',

'price' => 'required',

]); $product->update($request->all()); return redirect()->route('product.index')

->with('success', 'Product updated successfully.');

} /**

* Remove the specified resource from storage.

*

* @param \App\Models\Product $product

* @return \Illuminate\Http\Response

*/

public function destroy(Product $product)

{

$product->delete(); return redirect()->route('product.index')

->with('success', 'Product deleted successfully');

}

}

Basically, you have done the CRUD backend logic already. Take some time to go through the code, you will realize that we never ever write a single SQL code.

Step 4: Expose API

Lastly, we have done the backend CRUD, now we need to expose them into API so that frontend can call the API to manage the Product data.

Navigate to routes directory and open web.php file. Add in the line below:

Route::resource('product',ProductController::class);

To verify you have successfully exposed the API, run the following command: