query builder laravel code example

Example 1: how to get data from a table in laravel

use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')->select('id','name','email')->get();

        return view('some-view')->with('users', $users);
    }
}

Example 2: laravel not in query

DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();

Example 3: wherein laravel

DB::table('user')->whereIn('id', [100,200])->get();

Example 4: laravel wher in

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

Example 5: DB::table('users')->get();

$users = DB::table('users')->get();

Example 6: query builder laravel

use Illuminate\Database\Eloquent\Builder;

public function scopeFakePersons(Builder $query): Builder
{
  return $query->where('is_fake', 1);
}

Tags:

Html Example