Laravel 10 Ajax CRUD with Image Upload Tutorial Example

https://www.tutsmake.com/laravel-10-ajax-crud-with-image-upload-tutorial/

Laravel 10 Ajax CRUD with Image Upload Example

April 3, 2023 By Admin Leave a Commenton Laravel 10 Ajax CRUD with Image Upload Example

Laravel 10 ajax crud example with image upload. In this tutorial, we will show you how to create ajax crud app with image file upload and preview in Laravel 10 apps.

PlayUnmuteRemaining Time -33:46Auto(360pLQ)ShareFullscreenLaravel 8 image upload and display

This Laravel 10 ajax crud example with image file upload and preview tutorial guide you step by step on how to implement ajax crud app with image upload and preview using jquery, dataTable js, and bootstrap modal. This laravel ajax crud web app is not reloading the whole web page.

Laravel 10 Ajax CRUD with Image Upload Tutorial Example

Let’s use the following steps to implement ajax crud with image upload in laravel without page refresh; is as follows:

  • Step 1 – Install Laravel 10 App

  • Step 2 – Connecting App to Database

  • Step 3 – Create Migration And Model

  • Step 4 – Install Yajra DataTables In App

  • Step 5 – Add Routes

  • Step 6 – Create Controller

  • Step 7 – Create Blade View

  • Step 8 – Run Development Server

Step 1 – Install Fresh Laravel Setup

First of all, start your terminal to download or install Laravel 10 new setup. Run the following commands in it to install the new Laravel 10 app on your system:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Connecting App to Database

Next step, Set the database credentials in your application. Let’s open your project .env file and set the database credentials here.

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Recommended:- Laravel 10 PHP Guzzle Http Client GET & POST Example

Step 3 – Create Migration And Model

In this step, Execute the following command on terminal to create product table migration and create Product Modal using bellow command:

Navigate database/migrations/ and open create_products_table.php file. Then update the following code into this file:

Now run the following command:

php artisan migrate

This command will create tables in your database.

Recommended:-Next, Navigate to App directory and open Product.php file and then update the following code to into Product.php file as follow:Step 4 – Install Yajra Datatables Package in LaravelIn this step, Execute the following command to terminal to install Yajra Datatables Packages in your laravel application. Use the below command and install yajra packages in your Laravel 10 app:composer require yajra/laravel-datatables-oracleAfter successfully Install Yajra Datatables Packages in your laravel application. Next step, open config/app.php file and add service provider and aliases. config/app.php 'providers' => [ Yajra\Datatables\DatatablesServiceProvider::class, ], 'aliases' => [ 'Datatables' => Yajra\Datatables\Facades\Datatables::class, ] After set providers and aliases then publish vendor run by following command.php artisan vendor:publishRecommended:- Laravel 10 Ajax Image Upload with Preview Tutorial

Step 5 – Add Routes

In this step, Add routes in the web.php file as below.

Navigate to routes/web.php file and update the following routes:

use App\Http\Controllers\ProductController;

Route::get('product-list', [ProductController::class, 'index']);
Route::get('product-list/{id}/edit', [ProductController::class, 'edit']);
Route::post('product-list/store', [ProductController::class, 'store']);
Route::get('product-list/delete/{id}', [ProductController::class, 'destroy']);

Step 6 – Create Controller

In this step, create a new controller name ProductController. So use the below command and create a new controller that name is ProductController.

php artisan make:controller ProductController

Next navigate to app/Http/Controllers and open ProductController.php file. Then update the following methods for add products, edit product and delete the product into this controller file:

In Product Controller, you need to create some methods as follow:

  • Index()

  • Store()

  • Edit()

  • Destroy()

Index() method

Using the index() method, you will show the product list. So update the below code in your product controller index() method:

Store() Method

Using the Store() method, you will save and update the product into a database table. So update the below code in your product controller Store() method:

Edit() Method

Using the Edit() method, you will edit the product details, So update the below code with your edit method:

Delete() Method

Using the delete method, you can delete a product from the product list and also database table. So update the below code with your delete() method:

Now, update all methods into your ProductController.php file as follow:

Recommended:- Laravel 10 Auto Load More Data On Page Scroll

Step 7 – Create Blade View

In this step, create 3 blade views files. The first file is an action button that contains two buttons and the button name is edit and delete. The second file contains the product list. and the third file name image.blade.php file, it contains image.

Now, Navigate to resources/views and create action.blade.php file. This file contains a two-button name edit and delete. So you can update the below code in your action button file.

Next, create a list.blade.php file inside resources/views/ folder and update the below code in your product list file.

Next, you will create a script code for performing create, store, update and delete products from the database table products using jQuery ajax request in laravel. So update this code in your list.blade.php file:

Next, navigate to resources/views and create image.blade.php file. Then update the following code into your image.blade.php file:

Recommended:- Laravel 10 Ajax CRUD Using Datatable Tutorial

Step 8 – Run Development Server

Now, execute the following command on the terminal to start development server. So open your terminal and run PHP artisan serve command:

 php artisan serve
If you want to run the project diffrent port so use this below command 
php artisan serve --port=8080  

Now you are ready to run our this example, so open your browser and hit the below URL:.

http://localhost:8000/product-list

Conclusion

Laravel 10 ajax crud example with image upload and preview tutorial, you have learned how to create ajax crud example app with image upload preview using jQuery, yajra datatables and bootstrap modal without a refresh or reload the whole web page.

Last updated