V
V
Vatrush2019-05-10 15:49:41
Laravel
Vatrush, 2019-05-10 15:49:41

Why is undefined index happening?

If I output like this, then an undefined index error occurs when outputting to the view:

public function getUser()
    {
       $user = User::where('id','3')->get();
       return view('layouts/panel', compact('user'));
    }

If I output like this, then everything is fine:
public function getUser()
    {
       $user = User::find(3);
       return view('layouts/panel', compact('user'));
    }

Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2019-05-10
@Vatrush

In the first case, you have a collection (albeit from one element), and in the second, you have a model right away.
Most likely, you are trying to access some user attribute, but you are trying to get it on the collection - that's the error.
You didn't provide a view code or an index name, so there's nothing more to say.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question