我有这段代码:
$data = User::all(); //get all users
$providercounter = $data->pluck('provider'); //just pay attention to their provider column
if($providercounter == ""){
echo 'site'.$providercounter->count();
}ifelse($providercounter == "facebook"){
echo 'facebook'.$providercounter->count();
}
dd($providercounter);
我收到了这个错误:
Parse error: syntax error, unexpected 'echo' (T_ECHO)
在这一行:
echo 'facebook'.$providercounter->count();
我想获取我的提供者列数据,因此我可以在我的图表中使用它。 使用上面的代码,我尝试制作类似的内容:
echo site 20
//20 is count of users which their provider column is null
echo facebook 10
//10 is count of users which their provider column is facebook
所以我可以获得每个提供商的用户数。 (Facebook 10个用户),(网站20个用户)等。
我哪里错了?
感谢您的帮助,我现在有了结果,但没有达到预期效果:
这是我完成的代码
$data = User::all();
$providercounter = $data->pluck('provider');
if($providercounter == ""){
echo 'site'.$providercounter->count();
}elseif($providercounter == "facebook"){
echo 'facebook'.$providercounter->count();
}elseif($providercounter == "google"){
echo 'google'.$providercounter->count();
}elseif($providercounter == "linkedin"){
echo 'linkedin'.$providercounter->count();
}elseif($providercounter == "github"){
echo 'github'.$providercounter->count();
}
dd($providercounter);
我的结果是:
Collection {#634 ▼
#items: array:4 [▼
0 => null
1 => "google"
2 => "facebook"
3 => "facebook"
]
}
我的期望是:
site => 1
facebook => 2
所以它有我的回声,例如site, facebook, etc.
和他们的值的计数,如1, 2, ....
有什么想法吗?
答案 0 :(得分:4)
请尝试以下代码
您的问题是ifelse
,必须是elseif
$data = User::all(); //get all users
$providercounter = $data->pluck('provider');
if ($providercounter == "") {
echo 'site'.$providercounter->count();
} elseif ($providercounter == "facebook") {
echo 'facebook'.$providercounter->count();
}
dd($providercounter);
答案 1 :(得分:1)
尝试使用else if
if($providercounter == ""){
echo 'site'.$providercounter->count();
}elseif($providercounter == "facebook"){
echo 'facebook'.$providercounter->count();
}
答案 2 :(得分:1)
您遇到的错误是由于未使用其他或其他。
尝试使用
$data = User::all(); //get all users
$providercounter = $data->pluck('provider'); //just pay attention to their provider column
if($providercounter == ""){
echo 'site'.$providercounter->count();
}else if($providercounter == "facebook"){
echo 'facebook'.$providercounter->count(); } dd($providercounter);