[英]How to select the total downloads of each promotion that I have in my database, with laravel ORM eloquent 5.8?
Using laravel, I need to show in a graph the number of times a promotion is redeemed. 使用laravel,我需要在图表中显示兑换促销的次数。 I can get the promotions published by each user that starts session, but I need you to put that data in a graph. 我可以获取开始会话的每个用户发布的促销信息,但是我需要您将这些数据放在图表中。 I would greatly appreciate your help. 非常感谢您的帮助。
$nombres = Promociones::where('user_id',Auth::user()->id)->pluck('nombre_promocion', 'id');
$promociones = [];
$descargadas = [];
foreach ($nombres as $key => $val)
{
$promociones[] = $val;
$descargadas[] = Promociones::where('user_id',$key)->count();
}
$chart = new UserPromociones;
$chart->title(' Promociones publicadas ');
$chart->labels($promociones);
$chart->dataset('Numero de promociones','bar', ($descargadas));
$chart->dataset('Total de promociones publicadas = '.$total_promociones,'bar',[]);
i think the problem is in this line 我认为问题出在这条线
$descargadas[] = Promociones::where('user_id',$key)->count();
as you only look for user_id who match the array index!! 因为您只寻找匹配数组索引的user_id!
it should be like 它应该像
$descargadas[] = Promociones::where('user_id',$val)->count();
or 要么
$descargadas[] = Promociones::where('user_id',$val->id)->count();
and even if this work , that is not the best way, the worst thing to do is to but and database query in loop, that is horrible 即使这项工作不是最好的方法,但最糟糕的事情是,但是数据库查询处于循环中,这太可怕了
if the data is not that big , you can do something like that before foreach 如果数据不是那么大,您可以在foreach之前做类似的事情
$promociones = collect(Promociones::all()->toArray());
this will extract the model to a collection, then any where clause will be a search in a collection and require no database query for each time. 这会将模型提取到集合中,然后任何where子句都将在集合中进行搜索,并且每次都不需要数据库查询。
$promociones = collect(Promociones::all()->toArray());
foreach ($nombres as $key => $val)
{
$promociones[] = $val;
$descargadas[] = $promociones->where('user_id',$val->any_filed)->count();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.