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