This article goes in detail on laravel 9 ajax image upload. you will learn laravel 9 ajax image upload with validation tutorial. This tutorial will give you a simple example of how to upload image using ajax in laravel 9. We will look at an example of jquery ajax image upload laravel 9. So, let's follow a few steps to create an example of laravel 9 ajax post image upload.
In this example, we will create "images" table with a name column. Then we will create a form with file input, when you submit it will send the image via jquery ajax request and store the image into the folder and database.
So, let's see simple example and follow below steps:
Step 1: Install Laravel 9
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('images', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('images'); }};
Next, run create new migration using laravel migration command as bellow:
php artisan migrate
Now we will create Image model by using following command:
php artisan make:model Image
app/Models/Image.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model; class Image extends Model{ use HasFactory; protected $fillable = [ 'name' ];}
In this step, we will create a new ImageController; in this file, we will add two method index() and store() for render view and store images into folder and database logic.
Let's create ImageController by following command:
php artisan make:controller ImageController
next, let's update the following code to Controller File.
app/Http/Controllers/ImageController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use App\Models\Image; class ImageController extends Controller{ /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('imageUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); $imageName = time().'.'.$request->image->extension(); $request->image->move(public_path('images'), $imageName); Image::create(['name' => $imageName]); return response()->json('Image uploaded successfully'); }}
Furthermore, open routes/web.php file and add the routes to manage GET and POST requests for render view and store image logic.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ImageController; /* |--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ Route::controller(ImageController::class)->group(function(){ Route::get('image-upload', 'index'); Route::post('image-upload', 'store')->name('image.store');});
Step 5: Create Blade File
At last step we need to create imageUpload.blade.php file and in this file we will create form with file input button and written jquery ajax code. So copy bellow and put on that file.