Laravel 5: Fetch ajax data in route and pass to controller

23,120

Solution 1

I think the below example is what you're looking for

Route

Route::post('/getOrgById', 'HomeController@getOrgById');

Controller

public function getOrgById(Request $request) {
    $id = $request->input('id');
}

JS

var myJsonData = {id: 1}
$.post('/getOrgById', myJsonData, function(response) {
    //handle response
})

Solution 2

You should really look into resourceful controller actions. If you are wanting to fetch an organisation by its ID then you have an organisaiton entity, so create a corresponding organisation controller. This controller can then have a method to show an organisation by on its primary key value:

class OrganisationController
{
    public function show($id)
    {
        return Organisation::findOrFail($id);
    }
}

The route for this would look like:

Route::get('/organisations/{id}', 'OrganisationController@show');

You can then request this route via AJAX like so:

$.ajax({
    method: 'GET',
    url: '/organisations/' + id
});
Share:
23,120
Sharon Haim Pour
Author by

Sharon Haim Pour

SOreadytohelp

Updated on July 09, 2022

Comments

  • Sharon Haim Pour
    Sharon Haim Pour almost 2 years

    I'm using Laravel 5 and want to make ajax call to a controller with some data:

     $.ajax({
        url : "/getOrgById",
        data : JSON.stringify({id:1})
    })
    

    The routes.php has:

    Route::get('/getOrgById', 'HomeController@getOrgById');
    

    HomeController.php:

    public function getOrgById($data) {
       //code here fails with message 'Missing argument 1 for HomeController::getOrgById()
    }
    

    How can I pass the data from ajax to route and then to controller?

  • Sharon Haim Pour
    Sharon Haim Pour almost 8 years
    You are right in the example where I have only one variable. But what if I want to pass a complex json?
  • jeanj
    jeanj almost 8 years
    You can use Input::get() to retrieve your datas
  • Mauro Casas
    Mauro Casas almost 8 years
    As a demostration of the feature, nothing else. I also return 422 when form validation fails, using JSON.
  • Martin Bean
    Martin Bean almost 8 years
    A 422 should only be returned for validation failures. Given the copy-paste nature of Stack Overflow, you should not include setting something like a 422 status code on a successful request as a “demonstration” of the feature. It’s incorrect, and also had nothing to do with the question asked by the OP.
  • Mauro Casas
    Mauro Casas almost 8 years
    It's a technicallity we can all live with. In the end, I would be helping OP by letting him now he/she has multiple choices.
  • Martin Bean
    Martin Bean almost 8 years
    There are “choices” everywhere in code, but demonstrating status codes (which the question has nothing to do with) and demonstrating the wrong one on this occasion, is reckless and will do more harm than good with the “copy-paste” nature of Stack Overflow answers. Please be more careful in future. I’ve had to edit your answer because of this.
  • Yousaf Hassan
    Yousaf Hassan almost 7 years
    I think you forgot a "/" after getOrgById.