Powerful Applications with Laravel — Basic CRUD Operations

Gabriel Meireles
dev.meireles
Published in
2 min readMar 15, 2023
Photo by Robin Pierre on Unsplash

In a nutshell CRUD is an acronym widely famous in the world of computer programming that’s used to refer to basic operations known as Create, Read, Update and Delete. Throughout your life as a developer it’ll be really common to deal with models performing these functions.

Often, almost 100% as a rule controllers will interact with models, for example, in a e-commerce system where an user must be able to see a list of products we know there’s an entrypoint / route for products that’ll call the ProductController, in its turn will interact with the ProductModel that under the hood will execute a query on your system database, something usually like SELECT * FROM products, it could be the R as Read.

First of all, make sure to import the Product model use App\Models\Product

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{

C as Create

So let’s start implementing a functionality that’ll enable a user to create a new product on database:

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$product = new Product();
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->save();

return response()->json([
'success' => true,
'data' => $product,
]);
}

R as Read

Once we already have an inserted product on the database it’s time to retrieve / read it, for a list of items

/**
* Display a listing of the resource.
*/
public function index()
{
$products = Product::all();
return response()->json([
"success" => true,
"data" => $products,
]);
}

And for an individual item by id:

/**
* Display the specified resource.
*/
public function show(int $id)
{
$product = Product::find($id);

return response()->json([
'success' => true,
'data' => $product,
]);
}

U as Update

Now the update function that’ll update a current existent item:

/**
* Update the specified resource in storage.
*/
public function update(Request $request, int $id)
{
$product = Product::find($id);
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->save();

return response()->json([
'success' => true,
'data' => $product,
]);
}

D as Delete

And finally the delete that must remove an item based on id:

/**
* Remove the specified resource from storage.
*/
public function destroy(int $id)
{
Product::destroy($id);
}

As a rule of thumb these are the basic and primordial operations which will be your whole life partner as a developer, for sure, according to as your application increases and gets more complex, these functionalities probably will increase and turn on more complex so.

As you know, you can find the current updates on Github repository

--

--