如何使用Controller并返回查看总数量

问题描述 投票:0回答:1

enter image description here

在控制器中有一个dr_quantity的总和并返回到视图模型

public function searchmedreport()
    {

        $search = \Request::get('search');
        $total = DB::table('distribution_records')->where('id', Auth::user()->id)
        ->sum('medicine_name', 'LIKE', '%'.$search.'%');
        $records = DistributionRecord::whereRaw("Concat(dr_fname,' ',dr_lname) LIKE '%{$search}%' ")
        ->orWhere('medicine_name','LIKE','%'.$search.'%')
        ->orWhere('date_requested','LIKE','%'.$search.'%')
        ->orderby('id')->paginate(5000);

        return view('forms.searchmedreport',['records'=>$records,'total'=>$total]);
    }

<b><h4>Total Number of Medicine Distributed: {{$total}} </b></h4>

总计在

php laravel
1个回答
3
投票

根据您的表,您正在执行错误的查询,因此不是这样:

$total = DB::table('distribution_records')->where('id', Auth::user()->id)
             ->sum('medicine_name', 'LIKE', '%'.$search.'%');

你需要这个:

$total = DB::table('distribution_records')->where('user_id', Auth::user()->id)
             ->where('medicine_name', 'LIKE', '%'.$search.'%')->sum('dr_quantity');
© www.soinside.com 2019 - 2024. All rights reserved.