laravel select count code example

Example 1: select sum in laravel

Sometime for such queries you need to disable the strict check
 So inside config/database.php and inside mysql, 
 Set 'strict' => false,

->select('user_id', DB::raw('SUM(points) as total_points'))

Example 2: get count laravel

$count = Model::where('status','=','1')->count();

Example 3: parametre grouping laravel quert

DB::table('users')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();

Example 4: laravel join

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

Example 5: laravel wher in

$users = DB::table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();

Example 6: laravel select count

$count = DB::table('category_issue')->count();

Tags:

Php Example