Search code examples
phpjquerypostlaravellaravel-3

Missing argument error when posting data through ajax (using Laravel)


I'll be precise: I'm having a weird issue with $.post jQuery function and an action in my controller. I'm new to PHP, but I've worked with ASP.NET MVC before and I hoped that they were similar. Here is the problem:

//This is my ajax call
$.post('get_random_photos', count, function(data) {
    ...
});

//This is my action
public function post_get_random_photos($count) {
    ...
}

//This is what I have in my routes.php
Route::post('photos/get_random_photos', 'photos@get_random_photos');

When I make the ajax request, I get the following message:

Missing argument 1 for Photos_Controller::post_get_random_photos()

I know this could probably be solved using Input::get('count'); but I'd rather having that count as an action parameter, as I used to do in ASP.NET MVC. Is there any way of accomplishing this?


Solution

  • Your restful route needs to receive that count in the url, this is a way of doing this:

    //This is my ajax call
    $.post('get_random_photos/'+count, 'nothing', function(data) {
        ...
    });
    
    //This is my action
    public function random_photos($count) {
        ...
    }
    
    //This is what I have in my routes.php
    Route::post('photos/get_random_photos/(:num)', 'photos@random_photos');