Chonchol Mahmud
Chonchol Mahmud

Reputation: 2735

How to update database with AJAX & Laravel 5.2?

UPDATED QUESTION:

I want to update a database table column with AJAX and Laravel 5.2 framework. I have a button Delier when i will click on that button then it will update a column from Not Shipped to Shipped. I also using sweetAlert plugin for popup styling. I have searched a lot. But i didn't find perfect procedure of it. I have tried this way:

Routes:

Route::get('/winner/status/{id}', ['as' => 'winner.status', 'uses' => 'WinnerController@statusUpdate']);

WinnerController:

public function statusUpdate(Request $request, $id)
{
    $winner = Winner::find($id);
    $winner->product_stat = "Shipped";

    $winner->save();
    $request->session()->flash('alert-info', 'Product Status Updated!'); 
    return Redirect::to('admin/winner/detail');
}

Script in View:

$(".action-button").click(function(){    
swal({
  type : 'warning',
  title: 'Submit what you want',
  input: 'text',
  showCancelButton: false,
  confirmButtonText: 'Submit',
  showLoaderOnConfirm: true,
  allowOutsideClick: false

}).then(function (text) {

  $.ajax({
    type: "POST",
    url : '',
    success: function(data){
        swal('Confirmed!','success')
    }

  });
})

});

Blade:

@foreach($dataQrDetails as $dataQr)
    <tr>
        <td> {{  $dataQr->product_name }} </td>
        <td> {{  $dataQr->product_stat }} </td>
        <td> {{  $dataQr->created_at }} </td>
        <td> <a class="btn btn-info btn-xs action-button" href="{{route('winner.status',$dataQr->id)}}">Delier</a></td> 
    </tr>                            
@endforeach

Blade frontend:

enter image description here

This is updating column but after updated its redirected another page and its showing just popup its not need to submit confirm button of popup. Is there anyway to do this? Please could anyone answer my below question:

  • What will be the best procedure to using AJAX with Laravel.
  • What will be route call for update data?
  • How i define AJAX url?

Upvotes: 0

Views: 2507

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

What is do here is, when you load the form I'll save the user id in Hidden field and when i submit will pass that to controller as well

In Form

<input type="hidden" name="id" value="{{ $id }}">

In AJAX

$.ajax({
    url: "/update-winner",
    type:'POST',
    data: {_token:_token, id:id, .....},
    success: function(data) {
        if($.isEmptyObject(data.error)){
            swal('Confirmed!',data.success); # or swal('Confirmed!','success')
        }else{
            swal('error!',data.error); # or swal('error!','Errorrrrr')
        }
    }
});

In Route

Route::post('update-winner','HomeController@statusUpdate');

In Controller

function statusUpdate()
{

    <!-- if need  -->
    $validator = Validator::make($request->all(), [
        /....
    ]);

    if ($validate->fails())
    {
        return response()->json(['error'=>$validator->errors()->all()]);
    }
    else
    {
        $id = $request->input("id");
        $winner = Winner::find($id);
        $winner->product_stat = "Shipped";

        $winner->save();
        return response()->json(['success'=>'Added new records.']);
    }
}

Edit 01

In delete button add this data-delete="{{ id_fiedld_name}"

and in Ajax you can catch

var id = ($(this).data('delete'));

Upvotes: 0

Bilal Ahmed
Bilal Ahmed

Reputation: 4076

Many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser

best ways to pass variable in ajax request in laravel]

your route is

Route::get('testUrl/{id}', 'TestController@getAjax');

ajax request

<script>
  var Id = <?php echo $id; ?>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl/{id}',
                type: 'GET',
                data: { id: Id },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_GET['id'];
    return $id // any value return as a response 
}

Upvotes: 0

Related Questions