I'm trying to update a column in my database with a total of votes on a project. I'm having a 404 problem but I don't see the problem in my route. (PS: Laravel 3)
This is my Vote button on every project :
{{ Form::open('project/addvote', 'VOTE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Vote') }}
{{ Form::close() }}
So when you click on the vote button it use this route :
Route::put('project/addvote', array('uses'=>'projects@addvote'));
And this is my action in the projects controller (no updating yet, just trying to redirect) :
public function put_addvote(){
return Redirect::to_route('project', $id)
->with('message', 'Vote successful');
}
Redirecting to this route :
Route::get('project/(:num)', array('as'=>'project', 'uses'=>'projects@project'));
And this is getting me the 404 error
Thanks to every response and the great help here!
Actually, Redirect::to_route
expects a name of a route to redirect to it and a named route has to be declare with a name, like,
Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects@addvote'));
So, you can use it;s name to redirect to it like
return Redirect::to_route('project');
Here, project
has been used as it's name using 'as' => 'project'
. In, your example, you didn't gave any name to the route, here
Route::put('project/addvote', array('uses'=>'projects@addvote'));
The, as => 'route_name'
is missing.
For second question, you can do it as
$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();
It was a bit confusing but after a conversation through the commenting system the answer for routing is give below :
You (OP) mentioned that you have a route declared as
Route::get('projets', array('as'=>'projets', 'uses'=>'projets@index'));
To this route you are trying to redirect using this
return Redirect::to_route('project', $id);
So, you are passing a parameter and it's not in your route declaration and this is the problem, so to overcome this change your route declaration to this
Route::get('projets/(:num)', array('as'=>'projets', 'uses'=>'projets@index'));
Or
Route::get('projets/(:any)', array('as'=>'projets', 'uses'=>'projets@index'));
Or, you can make the param optional using a ?
, for example :
Route::get('projets/(:any?)', array('as'=>'projets', 'uses'=>'projets@index'));
You should have postd the original code with the question, anyways, Change this
return Redirect::to_route('project', $id);
to
return Redirect::to_route('project', array($id));