Laravel 5.3 Trying to get Property of non-object

Asked

Viewed 10,962 times

4

good morning! I followed what is said in this tutorial to fill values in a view: https://www.tutorialspoint.com/laravel/retrieve_records.htm

And I did the same scheme on my controller:

public function select_cursos_disciplinas()
{
    $cursos = DB::table('select * from cursos');
    $disciplinas = DB::table('select * from disciplinas');
    return view('cadastros.disciplinas_curso', ['cursos' => $cursos, 'disciplinas' => $disciplinas]);
}

In my view it’s like this:

<select class="form-control">
    <option disabled selected> --- </option>    
    @if ($cursos)
        @foreach ($cursos as $curso)
            <option> {{ $curso->nome }} </option>
        @endforeach
    @endif
</select>

However, it is giving the error "Trying to get Property of non-object". I already researched but I did not find the solution to what I need, because the solution was for only one value, but in my case I want several values.

What should I do to fill my select? Thank you

2 answers

3


It turns out that your method is not loading the data into the object. For this you need to apply the method get() at the end of the method table.

As in the example below:

public function select_cursos_disciplinas()
{
    $cursos = DB::table('select * from cursos')->get();
    $disciplinas = DB::table('select * from disciplinas')->get();
    return view('cadastros.disciplinas_curso')
           ->with('cursos', $cursos)
           ->with('disciplinas', $disciplinas);
}

Your foreach on view is right.

  • Thanks for the help Gumball. But it still gave the same error :/. I’m reading the log here and it seems that this DB returns an object of type stdClass. Does it have anything to do?

  • Haaa, truth. Put a ->get() at the end of the method. I will edit my code.

  • @Virgilionovic you could maybe give a timely time to give these kinds of comments. I already had that in mind and when I have a time I edit the answer.

  • Thanks. It worked. However, I had to change the string inside the table only to the table name, otherwise it would be an error. But you’ve already kicked the solution.

2

I think it’s best to use the Query Builder of Eloquent. Try it this way.

public function select_cursos_disciplinas()
{
    $curso = new Curso();
    $cursos = $curso->all();

    $disciplina = new Disciplina();
    $disciplinas = $disciplina->all();

    return view('cadastros.disciplinas_curso', 
        [
            'cursos' => $cursos, 
            'disciplinas' => $disciplinas
        ]
    );
}
  • 1

    I wonder why it’s better.

  • @Gumball in my opinion gets less verbose, simpler to understand, and decreases problems like sql inject, but it is my opinion. Eloquent is very good and his resources are great.

  • @Gumball it also decreases native sql writing within the method, so it gets less verbose and decreases the risk of sql injection.

  • I get it. It’s just that I don’t usually use either Query Builder or Fluent. Always Eloquent.

  • Query Builder is from Eloquent , I recommend taking a look, it gives a good agilized development, without writing native queries :), including using inside the entities. https://laravel.com/docs/5.3/eloquent-relationships#querying-Relations /----- https://laravel.com/docs/5.3/queries

  • Yes. I use these methods.

Show 1 more comment

Browser other questions tagged

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