In this post I will show laravel CRUD operation, I will show you step by step simple crud operation in laravel.
Step 1 - To Start a New Laravel Project:
Run the following command in a terminal to start a new Laravel project:
composer create-project --prefer-dist laravel/laravel YourProjectName
Step 2: Configure the database
Set up your database settings in your Laravel project's .env file.
Step 3 - Create Migration:
To create a migration for your database table, run the following command:
php artisan make:migration create_items_table
To specify your table schema, edit the migration file located in the database/migrations folder.
Step 4 - Run Migrations:
For creating the actual database table, run the migrations:
php artisan migrate
Step 5 - Create Model:
Create a model for your table using the following command:
php artisan make:model Item
This will create a model file in the app directory.
Step 6 - Create Controller:
Generate a controller for handling CRUD operations:
php artisan make:controller ItemController
A controller file will be generated and placed in the app/Http/Controllers directory.
Step 7 - Define Routes:
In the routes/web.php file define your routes for CRUD operations:
Route::resource('items', 'ItemController');
Step 8 - Implement Controller Methods:
Open ItemController.php and implement methods for Create, Read, Update, and Delete operations.
use App\Models\Item;
use Illuminate\Http\Request;
public function index()
{
$items = Item::all();
return view('items.index', compact('items'));
}
public function create()
{
return view('items.create');
}
public function store(Request $request)
{
Item::create($request->all());
return redirect()->route('items.index');
}
public function edit($id)
{
$item = Item::findOrFail($id);
return view('items.edit', compact('item'));
}
public function update(Request $request, $id)
{
$item = Item::findOrFail($id);
$item->update($request->all());
return redirect()->route('items.index');
}
public function destroy($id)
{
$item = Item::findOrFail($id);
$item->delete();
return redirect()->route('items.index');
}
→ Other Controller Method
use App\Models\Article;
use Illuminate\Http\Request;
public function index()
{
$articles = Article::paginate(10);
return view('articles.index', compact('articles'));
}
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'content' => 'required',
]);
Article::create($validatedData);
return redirect()->route('articles.index');
}
public function edit($id)
{
$article = Article::findOrFail($id);
return view('articles.edit', compact('article'));
}
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'content' => 'required',
]);
$article = Article::findOrFail($id);
$article->update($validatedData);
return redirect()->route('articles.index');
}
public function destroy($id)
{
$article = Article::findOrFail($id);
$article->delete();
return redirect()->route('articles.index');
}
Step 9 - Create Views:
Create Blade views for listing, creating, updating, and deleting items. You can place these in the resources/views directory.
Step 10 - Run Your Application:
Start the development server and access your application in a web browser:
php artisan serve