Darius
Darius

Reputation: 268

update table with laravel

I can`t update my table. Maybe you can see where is problem. Edit works fine. It brings value to the fields. If i erase {{ method_field('PUT') }} it saves values normally, but i need to UPDATE

It`s my UPDATE controller

public function update(Request $request, Radar $radar)
{
    Radar::update([
        'date' => $request->input('date'),
        'number' => $request->input('number'),
        'distance' => $request->input('distance'),
        'time' => $request->input('time'),            
        ]);

     return redirect('/radars');
}

Thats how my view looks like:

<form action="{{ url('/radars')}}" method="post" > 
{{ method_field('PUT') }}
{{ csrf_field() }}

Routes:

Route::put('radars/{radar}', 'RadarsController@update');

Error:

MethodNotAllowedHttpException
No message

Thank you for help.

Upvotes: 3

Views: 929

Answers (5)

Raza Mehdi
Raza Mehdi

Reputation: 941

If you are using resource route, then in your form, you need to change action from:

url('/radars')

to

route('radars.update', $radar)    

and keep the following:

{{ method_field('PUT') }}

Upvotes: 0

Zulfiqar Tariq
Zulfiqar Tariq

Reputation: 394

You need to add where statement to tell which record to update and dont

Radar::where('id',$id)->update([
        'date' => $request->input('date'),
        'number' => $request->input('number'),
        'distance' => $request->input('distance'),
        'time' => $request->input('time'),            
        ]);

return redirect('/radars');

Upvotes: 0

jvk
jvk

Reputation: 2201

Illuminate\Support\Facades\Request

Radar::update(Request::all());

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You need to specify ID:

{{ url('/radars/') . $radar->id }}

Also, you need to use object and not just a model class. Something like:

public function update(Request $request, Radar $radar)
{
    $radar->update($request->all());

    return redirect('/radars');
}

Upvotes: 1

Djave
Djave

Reputation: 9329

Looking at it, I think it should be like the below:

<form action="{{ url('/radars/' . $radar->id )}}" method="post" > 

If you look, you're posting to /radars but your route is radars/{driver}

Upvotes: 0

Related Questions