2

I can get array values on scrpit array .

enter image description here

Script is


 $(document).on('click', '#bulk_delete', function()
    {
        var id = [];

        if(confirm("Are you sure you want to  Delete this data?"))
        {
            $('.student_checkbox:checked').each(function(){
                id.push($(this).val());
            });
            if(id.length > 0)
            {
                $.ajax({
                    url:"del",
                    method:"get",
                    data:{id:id},
                    success:function(data)
                    {
                        alert(data);
                        $('#dTable').DataTable().ajax.reload();
                    }
                });
            }
            else
            {
                alert("Please select atleast one checkbox");
            }
        }
    });

How to get these array values to laravel controller and insert into the database

Route is

Route::get('/del', 'AjaxdataController@ins');

and AjaxdataController.php is

 public function ins(Request $request )
    {

    }
4
  • Did you call the controller? Here as per the above code you are calling the route.Right? Provide code of route as well as controller so it will be helpful. Commented Jan 16, 2020 at 4:37
  • Edited @SohilChamadia Commented Jan 16, 2020 at 4:43
  • Please check the answer that i had written.That may help you. Commented Jan 16, 2020 at 5:09
  • You need to pass this as a post request and change your route as well. Right now you are doing a get request so ideally the id will be passed in the URL to which your route is not capturing. Commented Jan 16, 2020 at 5:14

2 Answers 2

1

try to change method type of your ajax from get to post like this.

method="POST"

and change your route to post like this

Route::post('/del', 'AjaxdataController@ins');

and call id in your controller like this

public function ins(Request $request )
{
    $id = $request->id;
}

.

Sign up to request clarification or add additional context in comments.

Comments

0

Just write the below code in the controller.You will retrieve the id over there that is passed from ajax.

public function ins(Request $request )
    {
       print_r($request->id);
    }

You will get the id and then you can use the method of laravel to insert that data into the database.

Use this code in routes file instead of your code :

Route::post('/del/{id}', 'AjaxdataController@ins');

5 Comments

Change the method from get to post in your ajax script code as well as in your route file also.
all changed but no result.
What's the use of {id} in the route? OP obviously wants to insert multiple ids
As per the code of ajax script @TestCheck is trying to pass array of id so it can be pass as a parameter.
Parameters will be passed in the request. '/del/{id}' requires one parameter 'id' to be present in the URL, calling just /del will probably result in the URL not being found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.