Farshad
Farshad

Reputation: 2000

laravel 5 - sorting dates and searching the dates

I have a date table like below :

Schema::create('dates', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->dateTime('date');
    $table->float('price');
    $table->timestamps();
});

Now I have a form that submits an start and a finish date so I have 2 variables

$start_date;
$end_date

How can I sort my date table ascending and show the dates between the start_date and the end_date?

Upvotes: 0

Views: 286

Answers (2)

sepehr
sepehr

Reputation: 13

You can use Eloquent like below:

   YourModel::whereDateBetween(‘date’,$start_date,$end_date)->orderBy->(‘date’,’asc’)->get();

Upvotes: -1

thefallen
thefallen

Reputation: 9749

You can use the whereBetween() method:

\DB::table('dates')
    ->whereBetween('date', [$start_date, $end_date])
    ->orderBy('date', 'asc')
    ->get();

If you have a model for this table you can use it directly instead of the DB facade.

Upvotes: 2

Related Questions