Problems when performing a COunt in laravel 5.7

0

I have a function where I query a table with SQL and the result I see it, it works fine but I need to count the number of records that table has and I get an error.

public function myFunction($data)
{

    $data = DB::table('users')
        ->select('id', DB::raw('count(*) as total')) / también con: ->select(DB::raw('count(*) as total), ->select(DB::raw('count(id) as total)
        ->join('cities', 'users.city_id', "=", 'cities.id')
        ->join('provinces', 'cities.province_id', "=", 'provinces.id')
        ->where('code', $users)->get();


    return view('users', compact('data'));
}

I try to show the information in the view with {{$ data- > total_count}} and it shows me the following error: Undefined property: stdClass :: $ total_count (View: C: \ xampp \ htdocs \ index.blade. php)

    
asked by Joe 01.10.2018 в 20:42
source

1 answer

3

You could use the count () method as follows:

$data = DB::table('users')
    ->select('id', DB::raw('count(*) as total')) / también con: ->select(DB::raw('count(*) as total), ->select(DB::raw('count(id) as total)
    ->join('cities', 'users.city_id', "=", 'cities.id')
    ->join('provinces', 'cities.province_id', "=", 'provinces.id')
    ->where('code', $users)->count();

or Have your query

 $data = DB::table('users')
        ->select('id', DB::raw('count(*) as total')) / también con: ->select(DB::raw('count(*) as total), ->select(DB::raw('count(id) as total)
        ->join('cities', 'users.city_id', "=", 'cities.id')
        ->join('provinces', 'cities.province_id', "=", 'provinces.id')
        ->where('code', $users)->get();

and then call the data number

$data->count();

I refer you to the documentation of laravel 5.7 link

    
answered by 01.10.2018 в 21:14