在我的控制器函数中,有一个foreach循环,它使用查询生成器从数据库中获取数据,在foreach循环结束后,数据被传递到视图中。
class HomeController extends Controller {
public $menu;
public $msg;
public function __construct() {
$this->isEditor();
$this->menu = Config::get('constants.news.menu');
$this->msg = Config::get('constants.news');
}
public function index(Request $request) {
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
//dd(session()->all());
$sessval = session()->all();
$uroles = DB::table('users_roles')
->where('id', Auth::user()->id)
->where('role_id','4')
//->toSql();
->get();
foreach($uroles as $key => $value)
{
$urole[]=$value->col-name;
$companiesid[]=$value->col-name2;
$rpending=DB::table('')
->select('')
->where('')
->where('')
->whereRaw("")
->count();
$abc+=$rpending;
$cactivities=DB::table('')
->select('')
->where('','=','')
->where('','=',$value->)
->where('','=',$value->)
->count();
$def+=$cactivities;
$pactivities=DB::table('')
->select('')
->where('','=','0')
->where('','=',$value->)
->where('','=',$value->)
->count();
$mno+=$pactivities;
$uactivities=DB::table('')
->select('')
->where('','=','0')
->where('','=',$value->)
->where('','=',$value->)
->whereBetween('due_date', [$tfrom, $to])
->count();
$ghi+=$uactivities;
$delayed=DB::table('')
->select('')
->where('','=','1')
->where('','=',$value->)
->where('','=',$value->)
->whereRaw('`actual_date`>`due_date` ')
//->toSql();
->count();
$jkl+=$delayed;
}
return view('view.name', compact('abc', 'def', 'ghi', 'jkl', 'mno'));
}
任何可能的方式来获取数据在后台和当用户登录它将呈现视图更快。我还需要传递更多的数据到同一个视图和数据也是从数据库中使用forloop。如何减少加载时间?。请建议任何替代方法。
In view-
<div class="row">
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-green" style="background:green !important">
<div class="inner">
<h3>{{ $abc}}</h3>
<p>Comp</p>
</div>
<div class="icon">
<i class="fa fa-thumbs-up"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-orange" style="background: darkorange !important">
<div class="inner">
<h3>{{ $def}}</h3>
<p>Delayed</p>
</div>
<div class="icon">
<i class="fa fa-book"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-
arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-red"style="background: red !important">
<div class="inner">
<h3>{{ $ghi}}</h3>
<p>Pen</p>
</div>
<div class="icon">
<i class="fa fa-clock-o"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-aqua">
<div class="inner">
<h3>{{ $jkl}}</h3>
<p>Pending</p>
</div>
<div class="icon">
<i class="fa fa-user-plus"></i>
</div>
<a href="#" class="small-box-footer">More info <i class="fa fa-
arrow-circle-right"></i></a>
</div>
</div>
<!-- ./col -->
</div>
1条答案
按热度按时间but5z9lq1#
这里的问题是低效的数据库查询。在循环中查询通常会导致性能低下。
更好的方法是使用SQL连接或EloquentRelationships将所有相关数据提取到用户角色。