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

http://img1.mukewang.com/60a71d500001938b10950381.jpg

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>

把总数


呼啦一阵风
浏览 96回答 1
1回答

慕标5832272

根据您的表,您正在执行错误的查询,因此,请执行以下操作:$total&nbsp;=&nbsp;DB::table('distribution_records')->where('id',&nbsp;Auth::user()->id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->sum('medicine_name',&nbsp;'LIKE',&nbsp;'%'.$search.'%');你需要这个:$total&nbsp;=&nbsp;DB::table('distribution_records')->where('user_id',&nbsp;Auth::user()->id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->where('medicine_name',&nbsp;'LIKE',&nbsp;'%'.$search.'%')->sum('dr_quantity');
打开App,查看更多内容
随时随地看视频慕课网APP