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
Related
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();
...
I am beginner in Laravel. I have this code:
$data = Term::whereDate('begin_date', '>=', $start)->whereDate('end_date', '<=', $end)->get(['id','name','begin_date', 'end_date']);
This is work fine.
I need change my columns result: begin_date as start_date and end_date as finish_date
How can I change it?
You just need to pass the aliases in with the get selection
->get(['id','name','begin_date AS start_date', 'end_date AS end_date']);
You can write it like this one
Term::whereDate('begin_date', '>=', $start)
->whereDate('end_date', '<=', $end)
->get(['id','name','begin_date AS start_date', 'end_date As finish_date']);
Or by editing it in select method
Term::whereDate('begin_date', '>=', $start)
->whereDate('end_date', '<=', $end)
->select('id','name','begin_date AS start_date', 'end_date As finish_date')
->get();
Have you just tried replacing those columns in the code you pasted?
$data = Term::whereDate('start_date', '>=', $start)->whereDate('finish_date', '<=', $end)->get(['id','name','start_date', 'finish_date']);
You can name the columns inside get method on your query. Try this:
$data = Term::whereDate('begin_date', '>=', $start)->whereDate('end_date', '<=', $end)->get(['id','name','begin_date AS start_date', 'end_date AS finish_date']);
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();
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);
I have the following query, which does not give me the expected result:
$query = $query->join('events_dates', function($join) use ($data){
$join->on('events.id', '=', 'events_dates.event_id')
->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
->orWhere('recurrent', "=", 1)
->where((strtotime($data["date_start"]) - strtotime('event_dates.start_date')) % ('events_dates.repeat_interval' * 86400), '=', 0);
});
There are 4 where clauses in this query.
The requirement is that either the two first where clauses are executed, or either two last depending on the recurrentfield.
PHP returns an error division by zero, because the last Where clause should not be executed when recurrentis 0.
Any suggestions?
I don't know your exact goal nor do I know what your db looks so this is just a wild guess:
$query = $query->join('events_dates', function($join) use ($data){
$join->on('events.id', '=', 'events_dates.event_id')
->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
->orWhere('recurrent', "=", 1)
->whereRaw('DATEDIFF(?, event_dates.start_date) % event_dates.repeat_interval = 0', array($data['date_start']));
Update
This might help for the between two dates part
->where('events_dates.start_date', '<=', new Carbon($data['date_end']))
->where('events_dates.end_date', '>=', new Carbon($data['date_start']))