0

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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I could do that, but I need my ajax to be a post, any solution?
The same, actually, just pass a dummy value on post if you don't need to pass any more values than count. Or, as you said, maybe use the Input::get('count'). Edited.
Almost ok, I needed to change {count} by (:num), please fix your answer for further references and I'll accept it.
Edited and tagged as Laravel 3, I always forget to ask wich version.

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.