laradonk
2 years ago

Concept issue! "Trying to get property of non-object"

Posted 2 years ago by laradonk

I'm lost. I need to add a simple selectbox where the user can specify a parent item. (I do not want to make a relationship just for this box).

Based on the code below, if I remove the toArray() and add {{ $key }} to my view, I get an array of data as I would expect. But, the foreach does not work when I do {{$key->id}} I receive Trying to get property of non-object

As is, I get a list of IDs, but {{$key->id}} and {{$key->name}} gives "Trying to get property of non-object" and I don't know what that means or why it is not working.

PlaceController.php

public function create()
{
    $place = new Place;
    $parents = Place::pluck('name', 'id')->toArray();
    return view('places.create', compact('place','parents'));
}

create.blade.php

                        @foreach($parents as $key => $key)
                            {{$key}}
                        @endforeach

I'm sure this is less a technical issue than me not understanding a concept.

I thought Pluck was the right thing to use. I want to list from the database all of the Places that exist, I want to get only the name and the id. Formerly, I think I would use Lists() but this has since been deprecated and I was under the understanding that Pluck() was a replacement.

Please sign in or create an account to participate in this conversation.