简体   繁体   English

Laravel路由抛出NotFoundHttpException

[英]Laravel route throws NotFoundHttpException

I'm looking for some help. 我正在寻找帮助。 I've searched on other topics, and saw what is the problem approximatively, but didn't succeed to fix it on my code. 我搜索了其他主题,大致了解了问题所在,但未成功将其修复到我的代码中。 Now the question is: I have NotFoundHttpException when i try to submit an update on my code. 现在的问题是:当我尝试提交代码更新时,我遇到了NotFoundHttpException。

Here is the Controller and my function update 这是控制器和我的功能更新

<?php

namespace App\Http\Controllers;

use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;

class testing extends Controller
{
    public function index()
    {
        $user = T_collaborateurs_table::all();
        return view ("read", compact("user"));
    }
    public function create()
    {
        return view("create");
    }

    public function store(Request $Request)
    {
        T_collaborateurs_table::create(Request::all());

        return redirect("index");
    }

    public function show($id)
    {
        $user=T_collaborateurs_table::find($id);
        return view("show", compact("user"));
    }

    public function edit($id)
    {
        $user=T_collaborateurs_table::find($id);
        return view("update", compact("user"));
    }

    public function update(Request $Request, $id)
    {
        $user = T_collaborateurs_table::find($id);
        $user->update(Request::all());

        return redirect("index");
    }
}

Now the routes 现在的路线

Route::get("create", "testing@create");
Route::post("store", "testing@store");
Route::get("index", "testing@index");
Route::get("show/{id}", "testing@show");
Route::get("edit/{id}", "testing@edit");
Route::patch("update/{id}", "testing@update");

And now the view update.blade.php 现在查看update.blade.php

<body>
    {{Form::model($user, ['method'=>'patch', 'action'=>['testing@update',$user->id]])}}

    {{Form::label('Id_TCa', 'ID')}}
    {{Form::text('Id_TCa')}}
    {{Form::label('Collaborateur_TCa', 'collab')}}
    {{Form::text('Collaborateur_TCa')}}
    {{Form::label('Responsable_TCa', 'resp')}}
    {{Form::text('Responsable_TCa')}}

    {{Form::submit("update")}}
    {{Form::close()}}
</body>

Here the route:list 这里的路线:列表

I'm sorry if my words are not very understable... Thank you all for your time. 抱歉,如果我的话不是很稳定,谢谢大家。

{{Form::model($user, ['method'=>'PATCH', 'action'=>  ['testing@update',$user->id]])}}

Or try to use 'route' instead of 'action',to use 'route' you just need a little edit in your update route. 或者尝试使用“路由”而不是“动作”,要使用“路由”,您只需在更新路由中进行一些编辑即可。

Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing@update'));

in your view: 在您看来:

 {{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}

And please follow the convention of class naming. 并且请遵循类命名的约定。 Your class name should be 'TestingController' or 'Testing'. 您的类名称应为“ TestingController”或“ Testing”。

You could try method spoofing by adding 您可以尝试通过添加方法欺骗

{{ method_field('PATCH') }}

in your form and change the form method to POST 在您的表单中并将表单方法更改为POST

{{ Form::model($user, ['method'=>'POST', 'action'=>['testing@update', $user->id]]) }}

add the id as an hidden field 将ID添加为隐藏字段

{{ Form::hidden('id', $user->id) }}

access the id in the controller as 以以下方式访问控制器中的ID

public function update(Request $Request)
{
    $id = Input::get('id');
    $user = T_collaborateurs_table::find($id);
    $user->update(Request::all());

    return redirect("index");
}

also need to modify your route accordingly 还需要相应地修改您的路线

Route::patch("update", "testing@update");

尝试使用function update

return redirect()->route('index');

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

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