Laravel: Model-Form Binding Undefined variable

Asked

Viewed 280 times

0

Greetings, I’m new to using Standard, and at this time I use Standard 5.3,I can’t edit any data in my database and I’m using Route Model Binding. The mistake you make is "Undefined variable $senhor_found" of the object I passed through a view.

My routing:

Route::get('admin/procurarPaciente','AdminController@procurar_paciente');
Route::put('admin/editarPaciente','AdminController@editar_paciente');

My controllers:

   class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function procurar_paciente(Request $request){
    $id_pessoa=Auth::user()->id_pessoa;
    $pessoa=Pessoa::find($id_pessoa);
    $nome_pessoa=$pessoa->nome_pessoa;
    $encontrado=false;
    $senhor_encontrado=false;
    $nome_paciente=$request->get('nome_paciente');

    $pacientes=Pessoa::all();
        foreach($pacientes as $paciente){
            if($nome_paciente == $paciente->nome_pessoa){
                $encontrado=true;
                $senhor_encontrado=$paciente;
            }
        }


     if(!$encontrado){
         Session::flash('flash_message_paciente_error','Desculpe, mas o paciente não foi encontrado!');
         return view('sessoes.administrador')->with('nome_pessoa',$nome_pessoa);
     }else{
         return View::make('editar.paciente',compact('nome_pessoa','senhor_encontrado'));
     }
}

The method seek patient works and views the data in the form, but when you submit the data to edit the variable $senhor_encontrado is no longer recognised, the code of the form:

<div class="container">
    <h1 class="seccoes burner-light-blue">Editar um Paciente</h1>
    <hr><br>
    {!! Form::model($senhor_encontrado,array('url' =>'admin/editarPaciente','data-parsley-validate' => '','class' => 'burner-form' ,'method' =>'PUT')) !!}

     <input type="hidden" name="_token" value="{{ csrf_token() }}">
      <div class="form-group">
         {{  Form::label('name','Nome:')}}
         {{ Form::text('nome_pessoa',null,array(
                                                 'data-parsley-required' => 'true',
                                                  'class' =>'form-control',
                                                  'aria-describedby' => 'nome',
                                                  'placeholder'=>'Escreva o Nome do Paciente',
                                                  'data-parsley-length'=>'[3, 15]',

                                               )

         ) }}
     </div>
    <div class="form-group">
        {{  Form::label('name','Email:')}}
        {{ Form::text('email',null,array(
                                         'data-parsley-required' => 'true',
                                          'class' =>'form-control',
                                          'placeholder'=>'Escreva o Email do Paciente',
                                           'data-parsley-type'=>'email',
                                         )

        ) }}
    </div>
    <div class="form-group">
        {{  Form::label('name','Palavra-Passe:')}}
        {{ Form::password('password',array(
                                          'data-parsley-required' => 'true',
                                          'class' =>'form-control',
                                          'placeholder'=>'Escreva a Palavra-Passe do Paciente',
                                           'data-parsley-length'=>'[6, 20]',
                                           'id' => 'passwordPaciente',
                                         )

        )}}
    </div>
    <div class="form-group">
        {{ Form::label('name','Confirmar Palavra-Passe:')}}
        {{ Form::password('password-confirm',array(
                                           'data-parsley-required' => 'true',
                                           'type' => 'password',
                                           'class' =>'form-control',
                                           'placeholder'=>'Escreva novamente a Palavra-Passe do Paciente',
                                           'data-parsley-length'=>'[6, 20]',
                                           'data-parsley-equalto' =>'#passwordPaciente',
                                         )

        )}}
    </div>
    <div class="form-group">
        {{  Form::label('name','Morada:')}}
        {{ Form::text('morada',null,array(
                                                'data-parsley-required' => 'true',
                                                 'class' =>'form-control',
                                                 'aria-describedby' => 'morada',
                                                 'placeholder'=>'Avenida/Rua',
                                                 'data-parsley-length'=>'[3, 40]',
                                            )

        ) }}



{!! Form::close() !!}
</div>

I hope you help me. Thank you.

1 answer

1


First of all, this is very complex to understand, let’s assume that when you need a foreach to look for some data in your database, it is very sure to have something wrong, not that it will not work, but this may overload the server with unnecessary interactions.

So your controller should be something like:

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function procurar_paciente(Request $request){
    $id_pessoa=Auth::user()->id_pessoa;
    $pessoa=Pessoa::find($id_pessoa);
    $nome_pessoa=$pessoa->nome_pessoa;
    $encontrado=false;
    $senhor_encontrado=false;
    $nome_paciente=$request->get('nome_paciente');

    $senhor_encontrado = Pessoa::select('*')->where('nome_pessoa', '=', $nome_paciente)->get(1);



     if($senhor_encontrado->count() == 0){
         Session::flash('flash_message_paciente_error','Desculpe, mas o paciente não foi encontrado!');
         return view('sessoes.administrador')->with('nome_pessoa',$nome_pessoa);
     }else{
         return View::make('editar.paciente',compact('nome_pessoa','senhor_encontrado'));
     }
}

The modification I made here can be checked here: https://laravel.com/docs/5.3/queries#selects

What’s more, with the foreach, you do not pass the whole return object, only the part with the associative array, and it is a little difficult to affirm, but this may be the problem.

  • Thanks @Gabriel. I will change my code.

Browser other questions tagged

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