id
listing_id
start_date
end_date
1
1
2023-01-20
2023-01-25
2
1
2023-02-26
2023-02-10
3
1
2023-02-11
2023-02-20
4
1
2023-02-21
2023-02-27
$listings->whereHas(
'availabilityCalendar',
function ($query) use ($start_date, $end_date) {
$query->where('start_date', '>=', $start_date)
->where('end_date', '<=', $end_date);
})
}
Fetch data according to start_date = 2023-01-23 / end_date = 2023-02-20 I sent by frontend. The start_date and end_date values I sent are among the values in the records I wrote above.
If I sent start_date = 2023-01-01 / end_date = 2023-02-20 they wouldn't be included. Or if I had submitted the dates start_date = 2023-01-23 / end_date = 2024-01-01 they would still not be included.
But if I sent the dates start_date = 2023-02-01 / end_date = 2023-02-15 it would have been met.
Can you help with this?
You should make sure your query are casted as date.
You can do some thing like
$startDate = Carbon::createFromFormat('Y-m-d', $start_date);
$endDate = Carbon::createFromFormat('Y-m-d', $end_date);
$query->whereDate('start_date', '>=', $startDate )
->whereDate('end_date', '<=', $endDate );
or
$query->where(DB::raw('DATE(start_date)'), '>=', $startDate )
->where(DB::raw('DATE(end_date)'), '<=', $endDate );
Related
I currently have a query that sums the number of non-active users in the last year and groups them by country and city and then paginates the result:
UserData::query()
->select(
country,
city,
DB::raw('SUM(CASE WHEN end_date IS NULL THEN 0 ELSE 1 END) AS not_active'),
)
->whereBetween('created_at', [Carbon::now()->subYear(), Carbon::now()])
->groupBy('country', 'city')
->paginate('500');
But I also need to add to each group a column that shows how many active users are of the same group, of all time, not just last year:
UserData::query()
->select(
country,
city,
DB::raw('SUM(CASE WHEN end_date IS NULL THEN 1 ELSE 0 END) AS active'),
)
->groupBy('country', 'city')
->get();
Then in the frontend I want to display the data in a table so I want the data to be "merged" so that I can output the matching active and not_active columns together, so the end result should look like that:
country | city | active(all time) | not active(past year)
------------|-----------|------------------|-----------------------
Sweden | Stockholm | 25 | 1
Switzerland | Bern | 43 | 13
But how can it be done when using pagination?
I tried to do it with subqueries but that didn't work: (Note that I am using slightly different query checking whereNull for active users and whereNotNull for non-active users:
$result = UserData::select('end_date', 'country', 'city')
->where(function ($query) {
$query->select('end_date')
->whereNull('end_date')
->whereBetween('created_at', [Carbon::now()->subYear(), Carbon::now()]);
}, 'active')
->where(function ($query) {
$query->select('end_date')
->whereNotNull('end_date')
}, 'not_active')
->groupBy('country', 'city')
->paginate('500');
This can be solved by using a union()
$first = UserData::query()
->select(
'country',
'city',
DB::raw('SUM(CASE WHEN end_date IS NULL THEN 0 ELSE 1 END) AS not_active'),
)
->whereBetween('created_at', [now()->subYear(), now()]);
$second = UserData::query()
->select(
'country',
'city',
DB::raw('SUM(CASE WHEN end_date IS NULL THEN 1 ELSE 0 END) AS active'),
);
$first->union($second)
->groupBy('country', 'city')
->paginate('500');
Which will execute a SQL union query like select *cols* from *table* where *clause* union (select *cols* from *table* where *someOtherClause*)
I want to retrieve all the rows in the database which have a criteres_id to 30. Except that it is a log table, so I have several occurrences with the same products_id. I would like to recover the products_id which has the highest date.
my table :
products_id = 1 date = 10/12/2020
products_id = 2 date = 10/12/2020
products_id = 2 date = 4/10/2020
products_id = 3 date = 9/12/2020
products_id = 3 date = 1/12/2020
I want to take
products_id = 1 date = 10/12/2020
products_id = 2 date = 10/12/2020
products_id = 3 date = 9/12/2020
my request sql :
SELECT MAX( DATE ) , products_id, criteres_value
FROM `products_main_criteres_history`
WHERE `manufacturers_id`
IN ( 263962, 263961 )
AND `criteres_id` =30
GROUP BY products_id
I try to make that :
ProductMainCritereHistory::
select('products_id')
->max('date')
->where('criteres_id', '=', '30')
->where('manufacturers_id', '<', '1000')
->groupBy('products_id')
->get();
thks for your help
here's my solution :
ProductMainCritereHistory::
select('products_id')
->orderBy('date', 'desc')
->where('criteres_id', '=', '30')
->where('criteres_value', '>', '1')
->where('manufacturers_id', '<', '1000')
->groupBy('products_id')
->get();
You can try the given solution query. In this case, you can use groupBy to get the unique record only along with sort by date ascending so you'll get the desired result.
$data = ProductMainCritereHistory::where('criteres_id', '=', '30')
->where('manufacturers_id', '<', '1000')
->orderBy('date','asc')
->groupBy('products_id')
->get();
I have to search for data with given dates and time (morning and evening). How can I find available dates from data with laravel query?
If i search dates 2019-06-10 to 2019-06-18. It should only return id 3 and 4. In this query, it is returning id 1, 3 and 4. What's wrong with this query it is not checking from in between dates? I have date type column in the database for start and end date.
I want to check two conditions
Given dates should be not equal to date_from and date_to
Given dates are not between in these dates
Both conditions in same query. Hope you understand.
id event_id date_from date_to
1 1 2019-06-08 2019-06-12
2 1 2019-06-14 2019-06-16
3 2 2019-06-20 2019-06-25
4 3 2019-07-26 2019-07-27
$getEvents = Booking::where('category_id', '=', $categoryID)
->where('date_from', '!=', $startDate)
->where('date_to', '!=', $endDate)
->orWhere('date_from', '<', $startDate)
->orWhere('date_to', '>', $endDate)
->whereNotBetween('date_from', [$startDate,$endDate])
->whereNotBetween('date_to', [$startDate,$endDate])
->get();
Try this
DB::table('table_name')
->whereNotBetween('date', [star_date, end_date])
->get();
Assuming that $endDate will always be greater (or equal) than $startDate
$startDate = "2019-06-10";
$endDate = "2019-06-18";
Given dates are not between in these dates
->whereDate('date_to', '<', $startDate)
->whereDate('date_from', '>', $endDate)
If given $startDate is greater than db 'date_to', $startDate is not between 'date_to' and 'date_from'. Will retrieve records with 'date_to' less than "2019-06-10".
And when the given 'date_from' is greater than $endDate, $endDate is not between 'date_to' and 'date_from'. Will retrieve records with 'date_from' greater than "2019-06-18".
Given dates should be not equal to date_from and date_to
This is not needed since in the query of point 2, we use > and <, which will not take into account equals.
//->where('date_from', '<>', $startDate)->where('date_from', '<>', $endDate)
//->where('date_to', '<>', $startDate)->where('date_to', '<>', $endDate)
Just:
$getEvents = Booking::where('category_id', '=', $categoryID)
->whereDate('date_to', '<', $startDate)
->whereDate('date_from', '>', $endDate)
->get();
Laravel 5
$getEvents = Booking::where('category_id', '=', $categoryID)
->where('date_from', '<', $startDate)
->where('date_to', '>', $endDate)
->get();
I have Admission table which have id, school_id, start_date,end_date etc and i want to show single data if today's date is between start_date & end_Date in laravel eloquent
Applying below code you can get the latest data of current date
$currentDate = date('Y-m-d');
$result = my_table::where(DB::raw('DATE(start_date)'), '<=' , $currentDate)
->where(DB::raw('DATE(end_Date)'), '>' , $currentDate)
->orderBy('id','desc')
->first();
`$start_date = date('2018-01-01');
$end_date = date('2018-05-02');
Adminssion::whereBetween(Carbon\Carbon::now()->toDateString(), [$start_date, $end_date])->get();`
The carbon now() function will give you current date and time. And toDateString() function only give you today date. And with whereBetween() function you can compare the current date between start_date and end_date.
I would like to translate this sql request in query builder for laravel 4.2, could you help me?
Thanks in advance!
Table api_calls => id | key_id | server | created_at
Table api_key => id | key | created_at
SQL :
SELECT K.key FROM api_key as K
WHERE
(SELECT count(id) FROM api_calls WHERE server = $server AND key_id = K.id AND created_at >= $DateSeconds) < 10
AND
(SELECT count(id) FROM api_calls WHERE server = $server AND key_id = K.id AND created_at >= $DateMinutes) < 500
GROUP BY K.key
Use joins ang aggregation like that:
DB::table('api_key as K')
->select('K.*')
->join('api_calls as ac1', function ($join) use ($server, $DateSeconds) {
$join->on('ac1.server', '=', DB::raw($server));
$join->on('ac1.key_id', '=', 'K.id');
$join->on('ac1.created_at', '>=', DB::raw($DateSeconds));
})
->join('api_calls as ac2', function ($join) use ($server, $DateMinutes) {
$join->on('ac2.server', '=', DB::raw($server));
$join->on('ac2.key_id', '=', 'K.id');
$join->on('ac2.created_at', '>=', DB::raw($DateMinutes));
})
->having(DB::raw('COUNT(ac1.id)'), '<', 10)
->having(DB::raw('COUNT(ac2.id)'), '<', 500)
->groupBy('K.id')
->get();