Laravel how to query parent relation - php

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();

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();
...

Change column name in Laravel equivalent

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']);

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 / Eloquent WHERE NOT SUBQUERY

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);

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