how to create crud application in laravel 10 with examples complete code
09 August 2021
As of my last knowledge update in September 2021, Laravel 8 was the latest version available. However, Laravel 10 may have been released since then. I can provide you with instructions for creating a CRUD (Create, Read, Update, Delete) application in Laravel based on Laravel 8. You can adapt these instructions to Laravel 10 if needed. Here's a step-by-step guide with complete code examples:
Step 1: Set Up Laravel
First, make sure you have Laravel installed on your system. You can install it using Composer by running the following command:
bashCopy codecomposer global require laravel/installer
Save to grepperOnce installed, you can create a new Laravel project:
bashCopy codelaravel new my-crud-app
cd my-crud-app
Save to grepperStep 2: Create a Model
In Laravel, models are used to interact with your database. Let's create a model for a simple "Task" entity:
bashCopy codephp artisan make:model Task -m
Save to grepperThis command generates a Task model and a migration file for the database schema.
Step 3: Define the Database Schema
Edit the generated migration file (located in the database/migrations directory) to define the database schema for the "tasks" table. Here's an example migration:
phpCopy codepublic function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->timestamps();
});
}
Save to grepperRun the migration to create the "tasks" table:
bashCopy codephp artisan migrate
Save to grepperStep 4: Create Routes
Define routes in the routes/web.php file to handle CRUD operations:
phpCopy codeuse App\Http\Controllers\TaskController;
Route::resource('tasks', TaskController::class);
Save to grepperStep 5: Create a Controller
Generate a controller for the Task model:
bashCopy codephp artisan make:controller TaskController
Save to grepperIn the TaskController, you can define methods to handle CRUD operations. Here's an example:
phpCopy codeuse App\Models\Task;
use Illuminate\Http\Request;
public function index()
{
$tasks = Task::all();
return view('tasks.index', compact('tasks'));
}
public function create()
{
return view('tasks.create');
}
public function store(Request $request)
{
Task::create($request->all());
return redirect()->route('tasks.index');
}
public function show(Task $task)
{
return view('tasks.show', compact('task'));
}
public function edit(Task $task)
{
return view('tasks.edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect()->route('tasks.index');
}
public function destroy(Task $task)
{
$task->delete();
return redirect()->route('tasks.index');
}
Save to grepperStep 6: Create Views
Create Blade views for your application. Laravel Blade is a templating engine that allows you to create dynamic views.
- Create a view for listing tasks (resources/views/tasks/index.blade.php).
- Create a view for creating tasks (resources/views/tasks/create.blade.php).
- Create a view for displaying a single task (resources/views/tasks/show.blade.php).
- Create a view for editing tasks (resources/views/tasks/edit.blade.php).
You'll need to use HTML and Blade syntax to create these views. Display tasks, create forms, and handle user input accordingly.
Step 7: Test Your Application
You can now test your CRUD application by visiting the appropriate routes in your browser. For example:
- List tasks: http://localhost:8000/tasks
- Create a new task: http://localhost:8000/tasks/create
- View a task: http://localhost:8000/tasks/{task}
- Edit a task: http://localhost:8000/tasks/{task}/edit
Make sure you run php artisan serve to start the development server.
That's it! You've created a basic CRUD application in Laravel. Remember to adapt this code to any changes in Laravel 10, if necessary, based on the official documentation for that version.