如何使用控制器总计数量并返回查看

时间:2019-04-27 06:15:19

标签: php laravel

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>

输入总数

1 个答案:

答案 0 :(得分: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');