尝试在视图中获取非对象的属性'count' - Laravel6.18.6



我是web开发的新手,试图用laravel创建一个仪表板,并试图将我的数据传递给laravel,但我无法做到这一点,我得到了以上错误,

Trying to get property 'count' of non-object

那么,我如何在我的视图中检索计数数据和用户数据,这是我的控制器:

public function dashboard()
{
$countActiveUser = User::where('status',0)->get();
$countLatestUser = User::latest()->get();
$countBlockedUser = User::where('status',1)->get();
$countTotalUser = User::all();
$dataCount['activeUserCount'] = count($countActiveUser);
$dataCount['blockedUserCount'] = count($countBlockedUser);
$dataCount['latestUserCount'] = count($countLatestUser);
$dataCount['allUserCount'] = count($countTotalUser);
$data['users']=$countTotalUser;
$data['count']=$dataCount;
return view('dashboard',['data'=>$data,]);
}

以下是我如何尝试在仪表板中检索我的值。blade\

<span class="h2 font-weight-bold mb-0">{{$data->count->activeUserCount}}</span>

返回数据:

{
"data": {
"user": [
{
"id": 1,
"name": "user0",
"email": "nabrajkhadka43@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 2,
"name": "user1",
"email": "email1@gmail.com",
"phone": null,
"email_verified_at": "Apr 15 2020",
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": "Apr 22 2020"
},
{
"id": 3,
"name": "user2",
"email": "email2@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 4,
"name": "user3",
"email": "email3@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 5,
"name": "user4",
"email": "email4@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 6,
"name": "user5",
"email": "email5@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 7,
"name": "user6",
"email": "email6@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 8,
"name": "user7",
"email": "email7@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 9,
"name": "user8",
"email": "email8@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 10,
"name": "user9",
"email": "email9@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": null
},
{
"id": 24,
"name": "hari Khadka",
"email": "password@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": "2020-04-01-1324237408.jpg",
"lat": null,
"lon": null,
"bio": null,
"created_at": "Apr 01 2020"
},
{
"id": 26,
"name": "hari Khadka",
"email": "emaiasdl1@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": "2020-04-10-1742149546.jpg",
"lat": null,
"lon": null,
"bio": null,
"created_at": "Apr 10 2020"
},
{
"id": 27,
"name": "alfredaosd",
"email": "nabrajkhssadka43@gmail.com",
"phone": null,
"email_verified_at": null,
"profile_pic_url": null,
"lat": null,
"lon": null,
"bio": null,
"created_at": "Apr 10 2020"
}
],
"count": {
"activeUserCount": 9,
"blockedUserCount": 4,
"latestUserCount": 13,
"allUserCount": 13
}
}

}

我知道这是一个重复的问题,但请帮助我。

使用下面的代码在视图中查看刀片:

<span class="h2 font-weight-bold mb-0">{{$data['count']['activeUserCount']}}</span>

在您的刀片中逐个获取用户:

<table>
<tr>
@foreach($data['user'] as $user)
<td>{{ $user['id'] }}</td>
<td>{{ $user['name'] }}</td>
@endforeach
</tr>
</table>

使用以下代码。

$countActiveUser = User::query()->where('status',0)->count();
$countLatestUser = User::query()->latest()->count();
$countBlockedUser = User::query()->where('status',1)->count();
$countTotalUser = User::all()->count();

它将以整数形式返回计数。

最新更新