Hello Artisan
In this tutorial i am going to discuss about laravel and ajax. That mean now i will discuss about how we can use ajax in our laravel application to send http request.
Ajax request is a fundamental requirement of any php project to send http request to the server. By using Ajax request we can get or store data into database without page refresh. User feel more better to use this kind of features.
We will see how we can send laravel ajax post data to controller and get data from controller. We will also see about how we can learn about aravel ajax url route with parameters.
I already created many tutorial about laravel and ajax. I already created how to insert data using Ajax request in laravel 6. You can read it from below link.
Read also : Insert Data using Ajax Request in Laravel 6
But now in this laravel ajax example tutorial we will see how send Ajax post or get request in laravel 8. It is pretty simple and easy to use in laravel application. You have to follow some ew steps to learn how to use Ajax request in laravel 8.
Let's start our jquery ajax post request example in Laravel 8 tutorial.
Step 1: Create Route
firstly we have to create two route. One for view and another for sending http ajax request. Paste this below code
routes/web.php
Route::get('ajax', 'AjaxController@view');
Route::post('ajax', 'AjaxController@send_http_request')->name('ajaxRequest.post');
Step 2: Create Controller
Now we have to create controller to handle above two methods. So create controller and paste this following code.
app/Http/Controllers/AjaxController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function view()
{
return view('test');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function send_http_request(Request $request)
{
\Log::info($request->all());
return response()->json([
'success'=>'get your data'
]);
}
}
Since this is our testing tutorial on how to use laravel and ajax. So here we don't store our Ajax request data into database. We see it in log file.
Read also : Laravel 6 Ajax CRUD Example with Sweet Alert
Step 3: Create Blade File
finally we have to create our blade component to create html form. So now paste the below content to your test.blade.php file.
resources/views/test.blade.php
Now i you can run this code and visit those route url and give input in html form then you will get the below output.
storage/logs/laravel.log
[2020-03-27 03:54:19] local.INFO: array (
'name' => 'Mahedi Hasan',
'password' => '123456',
'email' => 'mahedy@example.com',
)
Read also : Laravel 6 Ajax Form Submit With jQuery Validation
Hope this laravel ajax example tutorial will help you.
#laravel #laravel-6 #laravel-7 #ajax #ajax-request #get-request #post-request #jquery