Laravel $Q->Where() Between Dates

Laravel $q-where() between dates

You can chain your wheres directly, without function(q). There's also a nice date handling package in laravel, called Carbon. So you could do something like:

$projects = Project::where('recur_at', '>', Carbon::now())
->where('recur_at', '<', Carbon::now()->addWeek())
->where('status', '<', 5)
->where('recur_cancelled', '=', 0)
->get();

Just make sure you require Carbon in composer and you're using Carbon namespace (use Carbon\Carbon;) and it should work.

EDIT:
As Joel said, you could do:

$projects = Project::whereBetween('recur_at', array(Carbon::now(), Carbon::now()->addWeek()))
->where('status', '<', 5)
->where('recur_cancelled', '=', 0)
->get();

How to filter data between two dates using eloquent laravel?

Try this :

$data = App\Models\Data::where('age_id',$age->id)
->where('nationality', $nationality)
->when(isset($to), function($q) use($from, $to){
$q->whereBetween('date', [$from, $to]);
})
->when(!isset($to), function($q) use($from){
$q->whereDate('date', $from);
})
->get();

Laravel Eloquent: How to use whereDate with Between?

For merging whereDate and between you can use something like this:

User::whereBetween(DB::raw('DATE(created_at)'), array($from_date, $to_date))->get();

sql :

select * from `users` where DATE(created_at) between '2018-02-01' and '2018-02-06'

Query Data Between Two Date from Date column in Laravel

Hope this answer helps

public function index(Request $request)
{

$user = auth()->user();

$expenses = Expense::whereHas('user', function($subQuery) use($user){
return $subQuery->where('shop_id', '=', $user->shop_id);
})->with(['user']);

if($request->start && $request->end) {
$expenses->whereBetween('date', array($request->start, $request->end));
}
return ExpenseResource::collection($expenses->get());

}

what you did was you get() the result first then you apply whereBetween().
Instead I applied whereBetween() first then if your if condition satisfied the get()

and change your URL to
127.0.0.1:8000/api/expense?start=2019-08-01&end=2019-08-04

Laravel Eloquent when date condition

You can use whereDate, here is additional info. However, your query needs some improvement. If you use when it is like an if statement. It checks your condition and if it is true then add query inside of its function.

If you create something like that:

  Attendance::where('employee_id', $request->employee_id)
->where('in_out', 'in')
->when($request->attendace_time, function ($q) {
return $q->where('attendance_time', Carbon::today());
})

That means if your request has attendace_time then add where query.

Or If you want to just filter todays attandees then you can use:

  Attendance::where('employee_id', $request->employee_id)
->where('in_out', 'in')
->whereDate('attendance_time', Carbon::today()->format('Y-m-d'));

Laravel Eloquent how to use between operator

$query->whereBetween('age', [$ageFrom, $ageTo]);

Look here: http://laravel.com/docs/4.2/queries#selects

Still holds true for Laravel 5: https://laravel.com/docs/5.8/queries#where-clauses

Laravel Eloquent find rows where date older than 2 days

You can use Carbon subDays() like below:

$data = YOURMODEL::where('created_at', '<=', Carbon::now()->subDays(2)->toDateTimeString())->get();

Getting specific date ranges from database in Laravel

You could create a scope for a particular class like this:

public function scopeYourQuery($query, $user) {
return $query->where('user_id', $user->id)->orderBy('created_at', 'desc')->first();
}

This just gets the first item of a descending ordered list ordered by created_at date per user.

If you wanted something that was between date ranges? You just pass in your date and extend it a bit with some PHP, maybe something like this would work:

public function scopeSomeDateQuery($query, $fetch_date, $user)
{
//clone the users chosen month - so we can make the range until the following month
$also_fetch_date = clone $fetch_date;
$next_month = $also_fetch_date->addMonth();
$next_month = $next_month->format('Y-m-d');
$fetch_date = $fetch_date->format('Y-m-d');
//return the query for the monthname
return $query->orderBy('created_date')->where('created_date', '>=', $fetch_date)->where('created_date', '<', $next_month)->where('user_id', $user->id);
}

This would look in a monthly range (per user) to get an ordered list of items with a created_date in that range.



Related Topics



Leave a reply



Submit