简体   繁体   中英

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.

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

<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'.

You could try method spoofing by adding

{{ method_field('PATCH') }}

in your form and change the form method to POST

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

add the id as an hidden field

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

access the id in the controller as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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