PHP Laravel AJAX Request GET and POST Example

Laravel Technology

Today, We want to share with you PHP Laravel AJAX Request GET and POST Example.In this post we will show you Laravel 5.7 Jquery Ajax Request Example From Scratch, hear for Ajax requests using jQuery and Laravel 5.7 we will give you demo and example for implement.In this post, we will learn about jQuery Ajax Call to Laravel Script with JSON Return with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

PHP Laravel AJAX Request GET and POST Example

There are the Following The simple About PHP Laravel AJAX Request GET and POST Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Simple Ajax request example with JQuery and Laravel, so the Laravel 5.7 Jquery Ajax Request Example From Scratch is following below.

Example 1 : Laravel Requests Ajax GET and POST

resources/views/product/create.php

    $(document).ready(function()
    {

        $( ".btn-product-save" ).click(function() {

            if($('#product_info').val() == '')
            {
                $(".product_infoerror").text("Enter Describe your Products Information here in details");
            }
            else{
                $("#loading").show();

                var productsobj = {};
                productsobj.user_id  = {{Auth::user()->id}};


                productsobj.product_info =$('#product_info').val();
                productsobj.productable_type = $('#productable_type').val();
                productsobj.product_id = $('#product_id').val();

                productsobj.product_team_display = 1;

                productsobj._token = '{{csrf_token()}}';


                $.ajax({
                    type:"POST",
                    dataType:"json",
                    url:"{{ url('products') }}",
                    cache: false,
                    data: productsobj,
                    success: function (data) {
                        //  console.log(data);
                    },
                    error: function (data) {
                        console.log('Error:', data);
                        var obj = {
                        };
                    }
                });
            }
        });
    });

routes/web.php

	Route::post('products', '[email protected]');

app/http/controllers/ProductController

	public function products(Request $request)
    {

          DB::table('productslist')->insert([
            'product_info' => $request['product_info'],
            'product_id' => $request['product_id'],
            'user_id' => $request['user_id'],
            'product_team_display' => $request['product_team_display'],
            'productable_type' => $request['productable_type'],
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);

        $product_id = DB::getPdo()->lastInsertId();

        $affected = DB::table('products')->where('id', '=', $request['product_id'])->update(array('productable_type' => 1));

        }

        $productslist = DB::table('productslist')
            ->join('users', 'productslist.user_id', '=', 'users.id')
            ->select('productslist.*', 'users.name AS uname')
            ->where('productslist.id', '=', $product_id)->get();
        return response()->json(['product'=>$productslist]);
    }

Example 2 : Laravel AJAX Calling For API

index.php

$( ".btn-product" ).click(function() {
    var productsobj = {};
    productsobj.product_id = $('#product_id').val();
    $.ajax({
        type:"POST",
        dataType:"json",
        url:"<?php echo $api_url.'updateproduct'?>",
        cache: false,
        data: productsobj,
        beforeSend: function(xhr){ xhr.setRequestHeader('Authorization', '<?php echo $your_secreate_key;?>'); },
        success: function (data) {
            //console.log(data['message']);
            window.location = 'index.php?res='+data['message'];
        },
        error: function (data) {
            console.log('Error:', data);
            var obj = {
            };
        }
    });
});

routes/api.php

Route::post('updateproduct', 'API\[email protected]');

app/http/controllers/API/ProductController

public function updateproduct(Request $request)
{
    $affected = DB::table('products')->where('id', '=', $request['product_id'])->update(array('status_id' => 0));
    return $this->sendResponse($request['product_id'], 'The Product has been updated!.');
}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Laravel AJAX Request GET and POST Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   how to loop through an array of objects in javascript?
  2.   7 Ways to Iterate Through ArrayList in Java Loop
  3.   Laravel 6 Ajax post data Example
  4.   PHP Laravel Eloquent Increments and Decrements value
  5.   Laravel controller Get csrf tokens in ajax calls
  6.   Laravel 5.7 JQuery AJAX Tutorial Example
  7.   Laravel check if ajax request or not example
  8.   jQuery Ajax POST example with Laravel 5.7
  9.   Laravel 6 JQuery Ajax Post Request Example
  10.   jQuery Multiple Tag Input with Dynamic Autocomplete Example

Leave a Reply

Your email address will not be published. Required fields are marked *