在刀片模板中,我有:
@foreach($users as $user)
{{ $user->id }} // this displays just fine, the number 1
{{ $weeklyCallsArray{$user->id} }} // get "Undefined offset:" error
{{ $weeklyCallsArray{1} }} // this works fine
@endforeach
如何让数组与相应的用户一起显示?我想使用类似的东西:
$weeklyCallsArray{user->id}
这是我的查询并在控制器中返回:
$users = DB::table('users')->get();
foreach ($users as $user) {
$userId = $user->id;
$countThisWeek = DB::table('calls')
->where('called_by', '=', $userId)
->where('calledd_on', '>=', Carbon::now()->startOfWeek())
->count();
$weeklyCallArray[$userId] = $countThisWeek;
}
return view('users')
->with(['users' => $users])
->with(['weeklyCallsArray' => $weeklyCallsArray]);
答案 0 :(得分:1)
我认为你在视图中使用了错误的数组?我以前从未见过这种语法。
$weeklyCallsArray[$user->id]
答案 1 :(得分:0)
你应该使用eloquent,你可以加载用户的电话。
awk '{
match($0, /MD:Z:[0-9A-Z\^]*/,m );
split(m[0],v,/[\^:]/);
nmatch = split(v[3],vmatch, /[^0-9]/);
cmatch=0;
for(i=1; i<=nmatch; i++) cmatch+=vmatch[i];
printf("%s"OFS"%.2f\n", $4, cmatch*100/(cmatch+nmatch-1));
}'
然后在您的视图中,您可以计算这样的来电:
$users = User::with(['calls' => function($q){
$q->calledd_on >= Carbon::now()->startOfWeek());
}]->whereUserId($id)->get();