I cannot correctly pass the arguments to the function that updates data in the database (Laravel)

Asked

Viewed 132 times

0

I am using Laravel 5.6 and came across a problem at the time I try to edit database records:

Step from the table that shows the data and options (edit, delete, etc.), to the edit page, and when I finish and step to the update function (below), an error is returned:

Too few Arguments to Function App Http Controllers Pagescontroller::update(), 0 passed and Exactly 2 expected

Function

public function atualizar($id, $corpo){
        $conteudo = Conteudo::find($id);
        $conteudo->corpo = $corpo;
        $conteudo->save();
        //return redirect()->route('');
    }

Page edit.blade.php

<form method="post" action="{{action('PagesController@atualizar', $id)}}">
                {{ csrf_field() }}     
                    <div class="field">
                        <input type="hidden" value="{{ $conteudo['id'] }}"/>
                        <textarea class="12u$" name="corpo" id="corpo">{{$conteudo['corpo']}}</textarea>
                    </div>
    <button type="submit" class="button small submit">Salvar
</form>

1 answer

1

When you submit the form, the data comes with the Request class.

Ideally you would add as a second parameter a request, already with dependency injection:

public function atualizar($id, Request $request){
    $conteudo = Conteudo::findOrFail($id);
    $corpo = $request->get('corpo');
    $conteudo->corpo = $corpo;
    $conteudo->save();
}

This way, you can get any data you add in the form.

Reference

  • Thank you for the answer! But I still find the same problem: "Too few Arguments to Function App Http Controllers Pagescontroller::update(), 1 passed and Exactly 2 expected"

  • In the form action, you can use Laravel’s route() helper. For this, go to your route file, find the route in question and call the additional method: ->route('nome.da.sua.rota');. In the form, do this: <form ... action="{{ route('nome.da.sua.rota', ['id' => $seu_id]) }}> Reference: https://laravel.com/docs/5.6/routing#named-Routes

  • When I access the variables through $_POST['x'], the code works. Function update: $conteudo = Conteudo::find($_POST['id']);&#xA; $conteudo->corpo = $_POST['corpo'];&#xA; $conteudo->save();

  • The following error is returned when using ->route('atualizar'); on the web.php: "Method Illuminate Routing Route::route does not exist."

  • Comments the entire route here.

  • Route::get('inicio', 'PagesController@inicio')->name('inicio');&#xA;Route::get('mostrar', 'PagesController@mostrar'); &#xA;Route::get('editar/{id}', 'PagesController@editar')->name('editar');&#xA;Route::post('atualizar', 'PagesController@atualizar')->route('atualizar');

  • The upgrade route has to be of the type PUT, and you missed to put the parameter {id} your route update as well. PS: With Laravel never use $_GET['any'], because the route work for the controller, does a whole security deal with the data.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.