how to create crud application in laravel 10 with examples complete code

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 code
composer global require laravel/installer
Save to grepper

Once installed, you can create a new Laravel project:

bashCopy code
laravel new my-crud-app cd my-crud-app
Save to grepper

Step 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 code
php artisan make:model Task -m
Save to grepper

This 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 code
public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description')->nullable(); $table->timestamps(); }); }
Save to grepper

Run the migration to create the "tasks" table:

bashCopy code
php artisan migrate
Save to grepper

Step 4: Create Routes

Define routes in the routes/web.php file to handle CRUD operations:

phpCopy code
use App\Http\Controllers\TaskController; Route::resource('tasks', TaskController::class);
Save to grepper

Step 5: Create a Controller

Generate a controller for the Task model:

bashCopy code
php artisan make:controller TaskController
Save to grepper

In the TaskController, you can define methods to handle CRUD operations. Here's an example:

phpCopy code
use 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 grepper

Step 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.

Leave A Comment