0

this has been driving me nuts...

I'm in my view I'm calling

Auth::user()->userlists

which brings me back an array of values like

{"id":"1","user_id":"1","name":"list1".....}

all I want to do is bring back the names I've tried all these;

Auth::user()->userlists->name
Auth::user()->userlists()->name
Auth::user()->userlists->name->get()
Auth::user()->userlists.name

But I always get an error such as "Undefined property"

How do I return this single property, it's in my array for all the items but I'm clearly just getting the syntax incorrect...?

The reason i'm trying to do this is that I need the values placed in a drop down box, it's finding the correct rows in the table but displaying all the data instead of just the name

Form::select('userlist_id', Auth::user()->userlists);

Many thanks.

4
  • Is userlists supposed to be a function in your User model or is it in attribute that would be in your database? Commented May 5, 2014 at 20:33
  • Userlists is a separate model from users. (It contains todo like lists) so when I call user->userlists it brings back the JSON of all the lists and their attributes belonging to the current user Commented May 5, 2014 at 21:16
  • In that case, I think user Royal Bg's answer should be helpful. Regular php array syntax won't work with json because it's tecnically just a string. Using json_decode() will convert it to an array you will be able to more easily use with php. Commented May 5, 2014 at 21:32
  • Can we see the view? Eloquent will automatically convert a model &/or collection to json if it is echoed, by way of the __toString() magic method. Commented May 5, 2014 at 22:48

1 Answer 1

1

You say a User has many Userlist models, so it's a collection not single model, thus you could work with it in a loop, BUT there is better way:

$lists = Auth::user()->userlists()->lists('name','id');

// To make it available in all the views, place this for example in a controller:
View::share('userlists', $lists);

This will fetch id and name for the related models collection and return it as an array, so it's just the one you will use in a Form builder:

Form::select('userlist_id', $lists)
Sign up to request clarification or add additional context in comments.

6 Comments

That looks like the kind of thing I'm after. I can't get it to work purely in the view however, so I probably need to make a method in the controller for it. I need this for all my views though so I have to figure out how to make something available to all views before I can try it
use View::share('userlist', $lists);
So the first part of your answer in the userlist controller with View::share underneath it? Then I should be able to call it in my nav on any page?
Check the edit. If you want it to be available in all the views you need it in a place that is called always. If it's for a single controller, then constructor will do
Downvoters, mind to explain what you don't understand about the answer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.