In the world of web development, CRUD (Create, Read, Update, Delete) operations are fundamental. Laravel, a PHP framework, simplifies the process of building web applications with its expressive syntax and powerful features. In this tutorial, we’ll walk through the process of creating a CRUD application in Laravel 8 that includes image upload functionality.
Prerequisites:
- Basic knowledge of PHP and Laravel.
- Composer installed on your system.
- PHP >= 7.3 and Laravel 8 installed.
Step 1: Setup Laravel Project
Firstly, open your terminal and run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel laravel-crud
Navigate into your project directory:
cd laravel-crud
Step 2: Create Migration and Model
Now, let’s create a migration for our database table. We’ll use artisan command to generate a migration file:
php artisan make:migration create_items_table
Open the generated migration file located at database/migrations
directory, and define the schema for your items
table:
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->string('image')->nullable();
$table->timestamps();
});
Run the migration to create the table in the database:
php artisan migrate
Next, let’s create a model for our Item
:
php artisan make:model Item
Step 3: Create Routes
Define routes for CRUD operations in routes/web.php
file:
use App\Http\Controllers\ItemController;
Route::resource('items', ItemController::class);
Step 4: Create Controller
Generate a controller using the following command:
php artisan make:controller ItemController --resource
Step 5: Create Views
Create views for displaying items, creating new items, editing items, etc. You can use Blade templating engine to create these views under resources/views
directory.
Step 6: Implement CRUD Operations
In your ItemController
, implement the CRUD operations using methods like index
, create
, store
, edit
, update
, and destroy
.
Step 7: Implement Image Upload
To enable image upload functionality, update your create
and update
methods in ItemController
to handle file uploads. Use Laravel’s file storage system to store uploaded images.
use Illuminate\Support\Facades\Storage;
...
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'description' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
]);
if ($request->hasFile('image')) {
$image = $request->file('image');
$imageName = time().'.'.$image->extension();
$image->storeAs('public/images', $imageName);
$validatedData['image'] = $imageName;
}
Item::create($validatedData);
return redirect()->route('items.index')->with('success', 'Item created successfully.');
}
Step 8: Display Images
In your views, display images using the <img>
tag and the appropriate path to the stored image.
Step 9: Test Your Application
Finally, test your CRUD application by creating, reading, updating, and deleting items. Ensure that image upload functionality works as expected.
Congratulations! You’ve successfully built a CRUD application with image upload functionality in Laravel 8. With Laravel’s powerful features and intuitive syntax, you can now expand and enhance your application further.
- Check our tools small Tools
- Check our tools website Word count