Hello artisan,
This article goes into detail on laravel 9 ajax image upload. From this laravel 9 ajax image upload tutorial, I am going to show you laravel 9 ajax image upload with validation tutorial. You will learn how to upload image using ajax in laravel 9. We will see some examples of jquery ajax image upload laravel 9. Just you have to follow a few steps to complete an example of laravel 9 ajax post image upload.
For this tutorial purpose, I will create an "images"
table with a name column. Then i will create a form with file input, when we submit it will send the image via jquery ajax request and store the image in the folder and database. So let's start laravel 9 ajax jquery file image upload to server and database:
Step 1: Download Laravel 9
In this first step, we need a fresh laravel application. run the below command:
composer create-project laravel/laravel example-app
Step 2: Create Migration and Model
Here, we will create migration for the "images" table, let's run the bellow command and update the code.
php artisan make:model Image -m
Now update the migration like:
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');
}
};
Now run php artisan migrate
command to create an images table. Now update the model:
app/Models/Image.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
}
Step 3: Create Controller
In this step, we will add two methods index() and store() for rendering view and storing images into folder and database logic.
app/Http/Controllers/ImageController.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');
}
}
Step 4: Create Routes
Now, open the routes/web.php file and add the routes to manage GET and POST requests for render view and store image logic.
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: Add Blade File
All are set to go. In this step, just we need to create an imageUpload.blade.php file and in this file, we will create a form with a file input button and written jquery ajax code. So copy it below and put it on that file.
resources/views/imageUpload.blade.php
Hope it can help you.
#laravel #laravel-9x #file-upload #jquery #ajax #image-upload