0

My controller is

public function index()
{
    $users = User::paginate(15);

    return view('dash.users.index')->with(array('users' => $users));

}

in view i pass: {{ $item->roles }} and return all column as array, if i put {{ $item->roles->get('name') }}the table is blank, why?

I want to show user->roles->name foreach user

user0111001101
  • 187
  • 2
  • 4
  • 11

2 Answers2

1

If it's an array, you should only simply use this:

{{ $item->roles['name'] }}
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
  • i try, but throw me an error: **Undefined index: name** – user0111001101 May 05 '16 at 17:47
  • @user0111001101, OK. So, please make a var_dump in your blade file: roles); die(); ?> – Mojtaba May 05 '16 at 17:52
  • @user0111001101, it's not the complete var_dump result. I need to know if it's an array or string. If it's a string, that means it's JSON and we could decode it – Mojtaba May 05 '16 at 18:06
  • @user0111001101 try this: {{ $item->roles()->toArray()['name'] }} or {{ current($item->roles()->toArray())['name'] }} – Mojtaba May 05 '16 at 18:14
  • @Mojtaba instead of going the long way via var_dump(); and then die(), you could just use dd(). That should save you a lot of typing ;) – Pascal May 05 '16 at 18:39
  • @Pascal, for this particular case, I don't like the format of dd(). Thanks – Mojtaba May 05 '16 at 18:40
0

solved with nested foreach! @foreach($item->roles as $item) {{ $item['name'] }} @endforeach

inside my @foreach user

Thank you Mojtaba for the help!

user0111001101
  • 187
  • 2
  • 4
  • 11