Laravel : Property [name] does not exist on this collection instance

Viewed 940

i'm trying to fetch some data from my users table, for my profile page. But when i click the profile page it give me some error :

Property [name] does not exist on this collection instance.

Where is the problem? am i do it wrong or some code missing?

User Model

protected $fillable = [
    'name', 'email', 'password', 'nim', 'phone_number', 'department'
];

protected $hidden = [
    'password', 'remember_token',
];

public function dropcv(){
    return $this->hasMany('App\Dropcv');
}

public function registration(){
    return $this->hasMany('App\Registration');
}

Controller

public function index()
{
    $users = User::all();
    return view('users.profile')->with('users',$users);
}

public function show($id)
{
    $users = User::find($id);
    return view('users.profile')->with('users',$users);
}

View

<h3>{{$users->name}}</h3>

In the view section i'm just do some test to fetch simple data from the users table.

i'm so frustrated finding the solution for my problem. i've search and try so many solution in the web but nothing works.i'm kinda new to laravel and really appreciate some help.

4 Answers

use to controller

public function show($id){
$users = User::find($id);
return view('users.profile',compact('users'));
}

use to view page

<h3>$users->name</h3>

use to controller

public function index(){
$users = User::all();
return view('users.profile',compact('users'));
}

You are trying to access a property on an array which is not correct you should foreach first then access your property

@foreach($users as $user)
<h3>{{ $user->name }}</h3>
@endforeach

You are trying to access a property on an array which is not correct
you should foreach first then access your property

@foreach($users as $user)
    <h3>{{ $user->name }}</h3>
@endforeach

Edit
When you use user::all or user::get() it's equivalent of select * from users sql syntax and laravel will give you an instance of collection instead of an array so when you call user::all() you get an instance of collection which has array of user instance in it here is an example for that

Collection {#332 ▼
  #items: array:3 [▼
    0 => User {#333 ▶}
    1 => User {#334 ▶}
    2 => User {#335 ▶}
  ]
}

for more information please read Laravel Collection

use this:

public function index()
{
    $users = User::get();
    return view('users.profile')->with('users',$users);
}

and for this controller you must use @foreach in blade for show users list, like this:

@foreach($users as $user)
    <h3>{{$user->name}}</h3>
@endforeach

and for function below:

public function show($id)
{
    $user = User::where('id', $id)->first();
    return view('users.profile')->with('user',$user);
}

and for this view:

<h3>{{$user->name}}</h3>

Actually i want to ask another question, when i use foreach it will show all data in my users table. is it possible to take only one specific data? i mean i only want to take one person data not the whole data.

Related