Laravel is one of the most popular PHP frameworks for building dynamic web applications. One of its core features is Eloquent ORM which allows developers to easily perform CRUD (Create, Read, Update, Delete) operations on a database.
Today we are going to discuss how we are going to perform these operations with Eloquent ORM.
Getting Set Up
Before we begin implementing CRUD functionality, we’ll need to take care of a few things:
Laravel Installation
We’ll be using the latest version of Laravel for this project. Make sure you have Laravel installed locally following the official installation instructions.
Database Setup
We’ll need a database server like MySQL or PostgreSQL running to store our application data. Create a new database and credentials.
Laravel Fundamentals
It will help to have a basic understanding of Laravel’s core features like routing, controllers and request handling. Feel free to refer to the docs if any concepts are unclear.
CRUD Concepts
Being familiar with basic CRUD (create, read, update, delete) operations and how they relate to common database tasks will serve as a helpful reference point.
Once these prerequisites are taken care of, we’ll be all set to bootstrap our sample Laravel CRUD application! Let me know if any part needs more explanation before we begin.
Also Read: Filament Vs Nova
Creating a Post Model
Models in Laravel are blueprints of tables in the database. They allow us to interact with database tables using object-oriented PHP.
To generate a Post model, we’ll use the Artisan make:model command:
php artisan make:model Post -m
The -m flag instructs Artisan to also create a migration file for defining the posts table structure.
This will create:
- App/Models/Post.php model
- Database migrations file
Let’s take a look at the Post model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
}
We’ll configure a few properties on the model class:
protected $table = 'posts';
This defines the associated database table name.
protected $primaryKey = 'id';
Specifies the primary key column name.
protected $fillable = ['title', 'body'];
Defines columns that can be mass assigned via arrays or objects.
This sets up the foundation for communicating with the posts table via the Post model.
Also Read: Email Template With Laravel Mail Message
Creating the Post Controller
Controllers in Laravel handle incoming web requests and determine corresponding responses. We’ll generate a PostController to handle POST actions:
php artisan make:controller PostController
This will create the App/Http/Controllers/PostController.php file.
By default it contains:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
}
Inside the controller class, we’ll define empty method stubs for the CRUD actions:
public function index() {}
public function create() {}
public function store(Request $request) {}
public function show($id) {}
public function edit($id) {}
public function update(Request $request, $id) {}
public function destroy($id) {}
These stub methods don’t contain any business logic yet. We’ll add that later using the Post model, validation and Eloquent queries.
For now, this establishes the route handler methods corresponding to typical resourceful controller actions as per RESTful conventions.
Also Read: How to Build E-commerce Store In Laravel
Creating the Database Table
Migrations in Laravel provide a convenient way to define and modify database schemas. We’ll generate a migration to scaffold our posts table:
php artisan make:migration create_posts_table
This will create a new migration file under the database/migrations directory.
Open the migration file and add columns to the up method:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
- id() defines the auto-incrementing primary key column
- title and body are data columns of string and text types
- timestamps() adds created_at and updated_at columns
To apply this schema definition to the database, run migrations:
php artisan migrate
This will create the posts table we defined.
Now that our table structure is in place, we can start building out the other components like models, controllers and views to perform actual CRUD operations on posts data in this table!
Also Read: Best Laravel Development Practices
Generating Blade Views
In Laravel, views contain the UI markup and logic to display information to the user. We’ll use Artisan to scaffold some basic views:
Home/Index View
This will list all posts:
php artisan make:view posts.index
Include a table or cards to display each post’s data.
Create View
For the submission form:
php artisan make:view posts.create
Add a form to collect and submit new post data.
Edit View
To update existing posts:
php artisan make:view posts.edit
Generate a pre-populated form to edit post details.
Show View
For individual post details:
php artisan make:view posts.show
Display a post’s information in detail.
These views will interact with the controller methods to display/submit data. We’ll pass post objects from the controller and utilize Blade directives like @foreach, @php and forms to build the UI. Finally linking views with named routes completes the MVC flow.
Also Read: Best Practices For Laravel Mobile App Development
Routing
Routes define the URLs to trigger controller actions. In routes/web.php:
Route::get('posts', [PostController::class, 'index'])->name('posts.index');
Route::get('posts/create', [PostController::class, 'create'])->name('posts.create');
This maps routes to their corresponding controller methods.
CRUD Operations
In the controller, add CRUD logic using Eloquent and Request validation:
public function store(Request $request) {
$request->validate([
'title' => 'required'
]);
Post::create($request->all());
return redirect('/posts');
}
Use models, queries, validation as needed per each action.
Also Read: Laravel Nova
Testing CRUD
To test it works:
- Run php artisan serve
- Visit routes in browser
- Try creating, editing, deleting posts
- Check data changes in database
This ensures proper routing, controller logic and database integration before deploying.
Fix any errors and you now have a fully functional CRUD system using Laravel out of the box!
Also Read: Best Security Features of Laravel
To End On
We hope this guide has provided you with a solid foundation for building CRUD functionality into your own Laravel applications using Eloquent ORM. Leveraging Laravel’s conventions and tools streamlines the development process.
If you found this introduction to basic CRUD operations with Laravel helpful, we recommend further exploring the framework’s robust features for Rest APIs and robust web applications. Resources like Laravel’s comprehensive documentation are also valuable tools in your journey as a PHP developer.
As your project needs grow, consider partnering with an experienced Laravel agency. Hybrid Web Agency is a leading solutions provider specializing in providing Laravel development Services. We offer flexible engagement models to suit diverse client needs – whether hiring in-house Laravel Developers or serving as a full-service outsourced development partner.
Hybrid’s seasoned Laravel experts deliver elegant, scalable solutions on time and on budget while maintaining the highest security and performance standards. Contact one of our consultants today for a free 30-minute discovery call to discuss how we can assist with your unique business goals and technical requirements. Together, we can help take your Laravel project to the next level.


