Laravel - Does not take the table column

Asked

Viewed 218 times

0

I’m in a quandary at Laravel I can’t fix, and I’ve tried a lot of things, and now I’m stuck with this: echo of a column value of my table in a input in the html.

Code in the Controller:

public function retorna_curso()
{
    $id_curso = Input::get('id_curso');
    $cursos = DB::table('cursos')->get();
    $select = DB::table('cursos')->where('id_curso', '=', $id_curso)->get();
    return view('editar.update_curso')
            ->with('select', $select)
            ->with('cursos', $cursos);
}

Code in View:

<form action="/updt_curso" method="post"> 
 <div class="form-group col-md-12">
    <?php              
       if(isset($select))
       {
            echo' <div class="form-group col-md-12">
            <label for="edt_nome">Editar nome do curso:</label>
                <input type="text" 
                       class="form-control" 
                       name="input_nomeCurso" 
                       value="'.$select['nome'].'" 
                       id="nomeCurso">';                    
            echo'
            </div>
            <div class="form-group col-md-4">
                <input type="submit" 
                       value="Atualizar" 
                       class="form-control btn btn-primary">
            </div>';
        }                   
    ?>
 </div> 
</form>

He enters the if, but makes an exception by saying that the property nome does not exist in the array $select that send back to the page.

  • What I could do to solve this problem?
  • What’s left to work?

1 answer

1


Use only one with with the array you want to send to the view:

...
return view('editar.update_curso')->with(['select' => $select, 'cursos' => $cursos]);

Remember that $select is an array of objects, in which each row is an object, you will have to select which Indice (line) you want, which in this case is the first because id I suppose is unique in the table, exchange your if for:

if(isset($select[0])) { // verificar se há alguma linha retornada

And in value is:

    ... value="' .$select[0]->nome. '" ...
  • 1

    It worked perfectly. I can’t believe it was that simple after so much stuff I tried kkkk. Thank you Miguel!

  • You’re welcome @Renan, thank goodness

Browser other questions tagged

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