Creationist Creationist - 1 year ago 72
PHP Question

Missing Argument 1 Laravel 4 Controller function

Okay, so a bit new to Laravel 4, and I am stuck on a problem.

I am using resource controllers on my routes, but I also have some other functions in my controller. As I said, I am new to Laravel, so I am not even sure that is proper practice.

So, my issue is, is that when I call the edit($id) function as a GET method, and then have the update($id) method as POST, it works fine.


Route::get('tasks/edit/{id}', 'TasksController@edit');
Route::post('tasks/edit', 'TasksController@update');

There routes that aren't working are:

Route::get('tasks/complete/{id}', 'TasksController@complete');
Route::post('tasks/complete', array('as' => 'tasks.completed', 'uses' =>'TasksController@completed')); //I've tried this route a few different ways

In my view, I'm calling the method with the Form::open() call like so:

{{ Form::open(array('route' => array('tasks.completed', $task->id))) }}

And in my TasksController.php my methods are:

* Complete the task
* @param int $id
* @return Response
public function complete($id) //GET

//Find the task by id and allow to complete
return View::make('tasks.complete')->with('task', Task::find($id));

* Update the completion
* @param int $id
* @return Response
public function completed($id) //POST

$tasks = Task::find($id);
$tasks->complete = Task::completion(); //scope query from Model

//Redirect to main tasks list
return Redirect::to('/');

No matter what I do, I get the continued error of: Missing Argument 1 for TasksController::completed().

I don't understand why the edit resource will work with no issues, yet the custom functions won't. I am almost positive I am overlooking something, but I just can't seem to figure out what.

Thanks for your help in advance!

Answer Source

You have to define the id parameter also in the POST route:

Route::post('tasks/complete/{id}', array('as' => 'tasks.completed', 'uses' =>'TasksController@completed'));

The difference in the form tag is subtle.

Without the ID on it it builds:

<form method="POST" action="http://site/tasks/complete?1" accept-charset="UTF-8">

And with it:

<form method="POST" action="http://site/tasks/complete/1" accept-charset="UTF-8">

But this is enough to the routing system not pass that parameter to your controller and then it's always missing.