Laravel 9 CRUD Example With Image Upload
Hello Artisan,
In this Laravel 9 crud with image upload tutorial, I will show you step by step laravel 9 crud with image upload example. You will learn from this example that how to create, create
, read
, update
and delete
in Laravel with image upload.
You just have to follow some few steps and you will get basic crud with image upload using controller, model, route, bootstrap 5, and blade. So you will learn crud example in Laravel 9 as well as Image upload in Laravel 9.
So let's start the tutorial:
Step 1 : Install Laravel 9
In the first step we need to get a fresh Laravel 9 version application using below command, So open your terminal OR command prompt and run below command:
composer create-project --prefer-dist laravel/laravel blog
Read also: Dynamic CRUD Example with Repository Design Pattern in Laravel
Step 2: Create Produt Model
In this tutorial, i am going to create crud with image upload laravel tutorial. So we are going to create a crud application for the product. so we have to create migration for "products" table using Laravel 9 php artisan command, so run below command to create product model.
php artisan make:model Product -m
And update your products table like below:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('detail');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
and update the Product model like below:
app/Models/Product.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'detail', 'image'
];
}
Step 3: Create Route
Here in this step, we need to add a resource route for the product crud application. so open your "routes/web.php" file and add the following route.
routes/web.php
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
Step 4: Create Controller
In this step, now we need to create a new controller as ProductController. in this controller we write logic to store images, and we store images in the "images" folder of the public folder. So run the below command to create a product controller.
php artisan make:controller ProductController --resource --model=Product
So, let's copy the bellow code and put it on the ProductController.php file and update it like that:
app/Http/Controllers/ProductController.php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}
Product::create($input);
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return view('products.show',compact('product'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'detail' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
$product->update($input);
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
}
Step 5: Create Blade Files
Almost we all are set to go. In this final step. In this step, we have to create just blade files. So mainly we have to create layout files and then create a new folder "products" then create blade files of the crud app. So finally you have to create the following bellow blade file:
- layout.blade.php
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
resources/views/products/layout.blade.php
resources/views/products/index.blade.php
resources/views/products/create.blade.php
resources/views/products/edit.blade.php
resources/views/products/show.blade.php
Now run php artisan serve
and visit the following URL:
url
Read also: Laravel CRUD With MongoDB Tutorial
Hope it can help you.