简体   繁体   English

Laravel更新数据库路由

[英]Laravel updating database route

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. 我遇到404问题,但在我的路线中看不到问题。 (PS: Laravel 3) (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 这让我得到404错误

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, 实际上, Redirect::to_route期望路由的名称重定向到该路由,并且必须使用一个名称声明一个命名的路由,例如,

Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects@addvote')); 路线:: 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'); 返回Redirect :: to_route('project'); Here, project has been used as it's name using 'as' => 'project' . 在这里, project已通过'as' => 'project'名称。 In, your example, you didn't gave any name to the route, here 在您的示例中,您没有给该路线起任何名字,

Route::put('project/addvote', array('uses'=>'projects@addvote')); 路线:: put('project / addvote',array('uses'=>'projects @ addvote'));; The, as => 'route_name' is missing. 缺少as => 'route_name'

For second question, you can do it as 对于第二个问题,您可以按照

$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();

Inserting & Updating Models . 插入和更新模型

Update : 更新:

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 您(OP)提到您有一条路线声明为

 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'));

Check wildcard Routes . 检查通配符路由

Update : 更新:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM