Laravel / Eloquent WHERE NOT SUBQUERY - php

I`m having trouble to setup a negative condition like such:
WHERE NOT( "last_day" "<=" $first_day OR "first_day" "<=" $last_day)
My query builder looks like this atm:
$query = $query->where(function ($query) use ($first_day, $last_day) {
$query->where('last_day', '<=', $first_day);
$query->orWhere('first_day', '<=', $last_day);
});
I would like it to be as such :
$query = $query->whereNot(function ($query) use ($first_day, $last_day) {
$query->where('last_day', '<=', $first_day);
$query->orWhere('first_day', '<=', $last_day);
});
To recap: I want an OR statement inside a negative WHERE condition. How can I accomplish this?
source : http://baodad.blogspot.nl/2014/06/date-range-overlap.html

You can play with the logic
$query = $query->where(function ($query) use ($first_day, $last_day) {
$query->where('last_day', '>', $first_day);
$query->where('first_day', '>', $last_day);
});

Went with reverting constraints according to answer found here: Eloquent MYSQL statement : WHERE NOT(A OR B)
$query = $query->where('last_day', '>', $first_day)->where('first_day', '>', $last_day);

Related

How to get data filter date according to field table from database

my code in LaporanController.php
class LaporanController extends Controller
{
public function index(Request $request)
{
if (isset($request->start_date) && isset($request->end_date)) {
$start_date = Carbon::parse($request->start_date)->format('Y-m-d');
$end_date = Carbon::parse($request->end_date)->format('Y-m-d');
$attendance_absent = DB::table('attendance_absent as absent')
->whereBetween('absent.do_date_start', 'absent.do_date_end', [$start_date, $end_date])
->get();
dd($attendance_absent);
}
}
}
how to get request data from start_date and end_date according to attendance_absent table from database fields do_date_start and do_date_end? i try to use whereBetween but i get error : Illuminate\Database\Query\Builder::whereBetween(): Argument #2 ($values) must be of type array, string given. how to solve my problem ?
normally whereBetween using for single column date check. but in this case youu need get from the different columns. so try to check those date like this. i think it will we help full for you.
do try it like this
->whereDate('do_date_start', '>=', $from_date)
->whereDate('do_date_start', '<=', $to_date)
->whereDate('do_date_end', '>=', $from_date)
->whereDate('do_date_end', '<=', $to_date)
other thin if you used whereBetween you will not get equal date of today.
whereBetween function is used to query one field with 2 or more values, what you really want is just where function twice, try this:
...
->where([
['absent.do_date_start', '>=', $start_date],
['absent.do_date_end', '<=', $end_date],
])
->orWhere(function ($query) use ($start_date, $end_date) {
$query->where([
['absent.do_date_start', '>', $start_date],
['absent.do_date_start', '>', $end_date],
['absent.do_date_end', '<', $start_date],
['absent.do_date_end', '<', $end_date],
])
})
->get();
...

Laravel Query Filtering Out Transactions and Checking Between Two Dates

My query is working for getting the existing transactions between two dates. However, the whereNotIn function is not filtering out the transaction. It still selects transactions that were either cancelled, declined or finished. What seems to be the problem on my query? Thank you
$associates = Associate::join('transactions', 'associate.associate_id', '=', 'transactions.associate_id')
->select('associate.associate_id')
->whereNotIn('status', ['Cancelled', 'Declined', 'Finished'])
->whereBetween('startdate',[$start_date, $end_date])
->orWhereBetween('enddate',[$start_date, $end_date])
->orWhere(function ($query) use ($request) {
$start_date = new DateTime($request->input('start_date'));
$end_date = new DateTime($request->input('end_date'));
$query->where('startdate','>=',$start_date)
->where('enddate','<=',$end_date);
})
->get();
you should orginize your 'wheres' to do exactly what you want, to do this, you can use where with closer:
$associates = Associate::join('transactions', 'associate.associate_id', '=', 'transactions.associate_id')
->select('associate.associate_id')
->whereNotIn('status', ['Cancelled', 'Declined', 'Finished'])
->where(function ($query) use ($request, $end_date, $start_date) {
$query->whereBetween('startdate',[$start_date, $end_date])
->orWhereBetween('enddate',[$start_date, $end_date])
->orWhere(function ($query) use ($request) {
$start_date = new DateTime($request->input('start_date'));
$end_date = new DateTime($request->input('end_date'));
$query->where('startdate','>=',$start_date)
->where('enddate','<=',$end_date);
});
});

How to use WhereNotIn and with in Laravel?

This is my Query eloquent created, i need to get materials that are available in dates.
I have guided from here:
Room Booking Query
How to fix these errors?
Material::with(['orders_material' => function($query) use ($begin_date,
$end_date, $begin_hour, $hour_final)
{
$query->whereNotIn(
$query->where('date_begin', '>=', $begin_date)
->where('date_begin', '<=', $end_date)
->where('date_final', '>=', $begin_date)
->where('date_final', '<=', $end_date)
->where('hour_begin', '>=', $begin_hour)
->where('hour_begin', '<=', $hour_final)
->where('hour_final', '>=', $begin_hour)
->where('hour_final', '<=', $hour_final);
//->wherePivot('Material_id', null)
//->wherePivot('Orders_id', null)
);
}
The syntax is not correct, what syntax I can use?
whereHas is the appropriate clause in this case.
Also, use whereBetween when querying for values falling between a min and max. It will simplify your query quite a bit:
Material::whereHas(['orders_material' => function($query) use ($begin_date, $end_date, $begin_hour, $hour_final) {
$query->whereBetween('date_begin', [$begin_date, $end_date])
->whereBetween('date_final', [$begin_date, $end_date])
->whereBetween('hour_begin', [$begin_hour, $hour_final])
->whereBetween('hour_final', [$begin_hour, $hour_final]);
})->get();

Laravel how to query parent relation

I have a Model ScheduledProgram that belongs to ProgramSession
I want to change the query below to replace registration_start_date (from ScheduledProgram) with registration_start (from ProgramSession). I.E. I want to change the query to get the field from its parent model instead of itself.
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->where('end_date', '>=', $today)
->get();
You're looking for whereHas():
$programs = ScheduledProgram::whereHas('programSession', function($query) use ($today) {
return $query->where('registration_start', '>=', $today)
->where('end_date', '>=', $today)
})
->get();

Eloquent - Where Date >= date(now) and whereNull('date')

I get all items from a table where:
endDate is >= now
endDate is NULL
published equals 1.
This is what I have, but it gives me 0 items:
$items = Items::orderBy(\DB::raw('RAND()'))
->where('endDate', '>=', date("Y-m-d"))
->whereNull('endDate')
->where('published', '1')
->whereIn('cid', $this->activeId)
->orderBy('id')
->paginate(4);
You need to use a closure and the orWhereNull():
->where(function($q) {
$q->where('endDate', '>=', date("Y-m-d"))
->orWhereNull('endDate');
})
You can do with two closure where and orWhere:
->where(function($query) {
$query->where('endDate', '>=', date("Y-m-d"))
->orWhere('endDate',NULL);
})
You are using both condition , which never give result
->where('endDate', '>=', date("Y-m-d"))
->whereNull('endDate')
Try to use orWhere

Categories