laravel sql code example

Example 1: laravel to sql

DB::table('users')->toSql()

Example 2: laravel sql query

use Illuminate\Support\Facades\DB;

$users = DB::select('select * from users');

foreach ($users as $user) {
    echo $user->name;
}

Example 3: laravel db::query update

DB::table('user')->where('email', $userEmail)->update(array('member_type' => $plan));

Example 4: laravel db query

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