简体   繁体   中英

Want to show total number of specific complaints of logged in user in laravel (blade)

Hey I have different complaints statuses in my table like active, closed etc. I want to total number of active complaints or closed complaints in my dashboard but I dont know how!

this is my controller which works good for total number of complaints to user and to admin:

    public function index()
    {
        if(auth()->user()->role=='USER') {
            $total = Complaint::where('user_id', auth()->user()->id)->count();
        } else {
            $total = Complaint::all()->count();
        }

        return view('dashboard', compact('total'));
    }

I simply use {{ $total }} to show the total number of complaints but now I want to show the total number of active complaints in the dashboard! how can I do that? Thanks

This should work:

$totals = DB::table('complains')
             ->select('status', DB::raw('count(*) as total'))
             ->where('user_id', auth()->user()->id)
             ->groupBy('status')
             ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM