laravel wherebetween

Posted by w3diy - 6 months ago

Laravel Eloquent whereBetween() Query Example

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $users = User::select("*")
                        ->whereBetween('points', [1, 100])
                        ->get();
        dd($users);
    }

Example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Carbon\Carbon;

class UserController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $start = Carbon::now()->subDays(30);
        $end = Carbon::now();

        $users = User::select("*")
                        ->whereBetween('created_at', [$start, $end])
                        ->get();

        dd($users);
    }
}