When embarking on application development, a firm grasp of CRUD functions becomes essential. In this article, I will delve into how Laravel handles CRUD operations. To begin with, CRUD stands for Create, Read, Update, and Delete – operations that find their necessity in most applications. Within Laravel, these functionalities are managed through Controllers.
Laravel uses the form element to facilitate the transfer of data from the frontend to the backend. Within this form
, you define input fields with the name
attribute, and you utilize various HTTP methods such as GET, POST, PUT, and DELETE to determine how the data should be processed on the server.
index() - GET
create() - GET
store() - POST
show() - GET
edit() - GET
update() - POST/PATCH
destroy() - DELETE
C - create() -> store()
R - index() / show()
U - edit() -> update()
D - destroy()
Controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ...
class MyController extends Controller {
public function index()
{
// Show all the data
// Search logic can be implemented here
In that case, we need "Request $request"
// Pagination logic can be implemented here
}
public function create()
{
// Show a form to create new data
}
public function store(Request $request)
{
// Store incoming data in the database
// Validation logic can be implemented here
// $request holds the input data
}
public function show($id)
{
// Show a single data
// $id is required to find the corresponding data
}
public function edit($id)
{
// Show the selected data to edit
// $id is required to find the corresponding data
}
public function update(Request $request, $id)
{
// Update the data
// $id and the input data/$request are required to
find and update the corresponding data
}
public function destroy($id)
{
// Delete the data
// $id is required to find the corresponding data
}
}
Let's say we'll create a book list and we have a Model Book.
And the view pages are as follows:
views/books
create.blade.php
edit.blade.php
index.blade.php
show.blade.php
<?php
namespace App\Http\Controllers;
use App\Models\Book;
use Illuminate\Http\Request;
class BookController extends Controller {
public function index()
{
// Query all the data
$books = Book::all();
// Pass the data to the index.blade.php view page with the data
return view('books.index', ['books' => $books]);
// OR using 'compact'
return view('books.index', compact('books'));
}
public function create()
{
// Display a form to create a new book
return view('books.create');
}
public function store(Request $request)
{
// Validate the incoming data
// If the data doesn't align with the rule, it won't proceed
$formFields = $request->validate([
'title' => ['required', 'string'], // OR 'required|string'
'author' => ['required', 'string'],
'comment' => ['required', 'string', 'max:300'],
'review' => ['required', 'numeric', 'min:1', 'max:5'],
]);
// Create new data
Books::create($formFields);
// Redirect to the index page
return redirect()->route('books.index');
}
public function show($id)
{
// Find the data by $id
$book = Book::where('id', $id)->firstOrFail(); // Queries the database for a book with the specified ID
// OR
$book = Book::find($id); // Directly queries the database for a record by its primary key
// OR
$book = Book::findOrFail($id); // Retrieve a record by its primary key
return view('books.show', compact('book');
}
public function edit($id)
{
// Find the data by $id
$book = Book::findOrFail($id);
// By passing the data, a user can see the old data when editing
return view('books.edit', compact('book'));
}
public function update(Request $request, $id)
{
// Validate the incoming data
$formFields = $request->validate([
'title' => ['required', 'string'], // OR 'required|string'
'author' => ['required', 'string'],
'comment' => ['required', 'string', 'max:300'],
'review' => ['required', 'numeric', 'min:1', 'max:5'],
]);
// Find the data by $id
$book = Book::findOrFail($id);
// Update the data
$book->update($formField);
// Redirect to the show page
return redirect()->route('books.show', $book);
}
public function destroy($id)
{
// Find the data by $id
$book = Book::findOrFail($id);
// Delete the data
$book->delete();
return redirect()->route('books.index');
// OR redirect to the index page with a success message
return redirect()->route('books.index')->with('success', 'Book has been deleted successfully');
}
Today, I discussed the fundamental aspects of CRUD functions within Laravel. Laravel has introduced more straightforward methods of writing code, like Eloquent ORM. This means that there isn't just one single "right" approach, which is quite impressive. Yet, as a beginner, this variety of options can sometimes be confusing, particularly when working on larger projects. Progressing from the fundamental concepts to the more advanced ones gradually would enable me to grasp concepts more swiftly and effectively.
Top comments (0)