Find Expiry date of Deal in relation table - php

I have a category table in which I have different categories of deals. Each of them consists of many deals along with its expiry date. I want to access only those deals with their categories whose expiry date is not over, but I am getting an issue that if any deal of category exists in time range all of its deals arrive whether it's expired or not. Here is my code:
$deals = DealCategory::where('name', '!=', 'Today Deal')
->whereRelation('deals','start_date', '<=', date('Y-m-d'))
->whereRelation('deals', 'expiry_date',">=", date('Y-m-d'))
->with('deals', 'deals.deal_images', 'deals.deal_products', 'deals.deal_products.product', 'deals.rating')->latest()->Paginate(12);
return response()->json(['Deals' => $deals, 'Date' => Carbon::now(), 'status' => 'success'], 200);

When you eagerly load relations using with, you can pass additional criteria to tell Eloquent which records to load:
DealCategory::where('name', '!=', 'Today Deal')
->whereRelation('deals','start_date', '<=', date('Y-m-d'))
->whereRelation('deals', 'expiry_date',">=", date('Y-m-d'))
->with(['deals' => function ($query) {
$query->where('start_date', '<=', date('Y-m-d'));
$query->where('expiry_date',">=", date('Y-m-d'));
$query->with('deal_images', 'deal_products', 'deal_products.product', 'rating');
}])
->latest()->Paginate(12);
Latest versions of Laravel even include a dedicated withWhereHas method to check for the existence of a relationship while simultaneously loading the relationship based on the same conditions:
DealCategory::where('name', '!=', 'Today Deal')
->withWhereHas('deals', function ($query) {
$query->where('start_date', '<=', date('Y-m-d'));
$query->where('expiry_date',">=", date('Y-m-d'));
$query->with('deal_images', 'deal_products', 'deal_products.product', 'rating');
})
->latest()->Paginate(12);
Either option should do what you need.

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 fetch sum of collection with leftJoin between two dates

I want to list the revenue of all businesses. All businesses are meant to be listed whether they have revenue or not. The revenue can also be sorted by date (within a date range). Meaning that I can check how much each of the business has made between certain dates.
Here is my implementation below:
return Business::
leftJoin('invoice as i', 'i.business_id', '=', 'businesses.id')
->leftJoin('departments as d', 'businesses.department_id', '=', 'd.id')
->where('i.created_at', '>', $from)
->where('i.created_at', '<', $to)
->select('businesses.id','businesses.name', 'd.name as department', 'd.id as deparment_id', DB::raw('coalesce(sum(i.total_amount_paid),0) as received'),
DB::raw('coalesce(sum(i.invoice_amount),0) as expected'))
->groupBy('businesses.id', 'businesses.name')
->get();
This works but it doesn't return all the businesses. I need all business returned irrespective of whether there are invoices during the set date duration or not.
It only returns all businesses when the snippet below is not within the query builder and that way I cannot sort by date:
->where('i.created_at', '>', $from)
->where('i.created_at', '<', $to)
You should add the date filtration into the join condition as following:
return Business::
->leftJoin('invoice as i', function($join)
{
$join->on('i.business_id', '=', 'businesses.id'));
$join->on('i.created_at', '>', $from);
$join->on('i.created_at', '<', $to);
})
->leftJoin('departments as d', 'businesses.department_id', '=', 'd.id')
->select('businesses.id','businesses.name', 'd.name as department', 'd.id as deparment_id', DB::raw('coalesce(sum(i.total_amount_paid),0) as received'),
DB::raw('coalesce(sum(i.invoice_amount),0) as expected'))
->groupBy('businesses.id', 'businesses.name')
->get();

Filter elements by counting their relationship Laravel5

I have two tables, the typical master-detail, what I intend to do is obtain the master's records, while the registration number obtained by means of a where of the relationship is greater than n
I've been doing this until now
Master::withTrashed()
->with('details')
->withCount(['details' => function ($query) {
$query->whereDate('date_init', '<', Carbon::now()->toDateString());
}]);
with this I get the master and its details with the given conditions, but there are teachers that the count of the relationship is 0, that I want to ignore. How can I do this?
What you are looking for is the following query:
Master::withTrashed()
->with(['details' => function ($query) {
$query->whereDate('date_init', '<', now());
}])
->whereHas('details', function ($query) {
$query->whereDate('date_init', '<', now());
})
->get();
The query literally means:
Give me all Masters, also the deleted ones, which have at least one attached Detail with an initialization date in the past. Also eager load all attached Details which have been initialized in the past.
Master::withTrashed()
->has('details')
->with('details')
->withCount(['details' => function ($query) {
$query->whereDate('date_init', '<', Carbon::now()->toDateString());
}]);
Fixed whereHas to has, as pointed out by Namoshek.

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 date range between range

I am building a website where i have a booking mechanism.
The user can book a hotel room for X days if the room is available.
I am using:
Laravel 5.4
MySql
The room is unavailable if:
It is already booked by another user
The admin has set it as unavailable
The room capacity is less or equal to the number of travellers
If have 3 tables to store those data:
Rent: Contains the booking infos, such as rentStartDate and rentEndDate (as DateTime) and other fields (userId, rentalId, ...)
Unavailabilities: When the admin set a room as unavailable, it's stored here. I have the fields unavailabilityStartDate, unavailabilityEndDate (as DateTime) and rentalId
Rentals: This table contain all the infos regarding the room (capacity ,name, location, ...)
I am struggling to build a Eloquent query to check if the room is available before processing the user payment. Here is what i have for now:
public function isAvailableAtDates($rentalId, $startDate, $endDate, $capacity) {
$from = min($startDate, $endDate);
$till = max($startDate, $endDate);
$result = DB::table('rentals')
->where([
['rentals.rentalId', '=', $rentalId],
['rentals.rentalCapacity', '>=', $capacity]
])
->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
$query->select('unavailabilities.rentalId')
->from('unavailabilities')
->where([
['unavailabilities.rentalId', '=', $rentalId],
['unavailabilityStartDate', '>=', $from],
['unavailabilityEndDate', '<=', $till],
]);
})
->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
$query->select('rent.rentalId')
->from('rent')
->where([
['rent.rentalId', '=', $rentalId],
['rentStartDate', '>=', $from],
['rentEndDate', '<=', $till]
]);
})
->select('rentals.rentalId')
->get();
return count($result) == 1;
}
Let's say I have a row inside Unavailabilities with:
unavailabilityStartDate = 2017-04-26 00:00:00
unavailabilityEndDate = 2017-04-30 00:00:00
When calling the method with some dates outside of the range stored in Unavailabilities, i'm getting the expected result. When calling it with the exact same dates, i'm getting no result (which is what i want).
So far so good!
The problem is if i'm calling it with a start date between 26 of April and 30th and a end date later in May, i am still getting a result even tho i shouldn't.
Could anyone help me with that?
This is not a laravel, nor a mysql issue.
try this:
->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
$query->select('unavailabilities.rentalId')
->from('unavailabilities')
->where([
['unavailabilities.rentalId', '=', $rentalId],
['unavailabilityStartDate', '<=', $till],
['unavailabilityEndDate', '>=', $from],
]);
})
->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
$query->select('rent.rentalId')
->from('rent')
->where([
['rent.rentalId', '=', $rentalId],
['rentStartDate', '<=', $till],
['rentEndDate', '>=', $from]
]);
})
You need all rent and unavailabilities records that had been started before $till and hadn't been ended before $from date.
Try to draw a time diagram.

Categories