Retrieve data from the database using Eloquent between two dates - php

I'm having trouble retrieving data from the database using Eloquent. I am trying to retrieve all data between two dates (created_at field).
I've done this:
public function search(Request $request){
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('Date_Occured',[$start_date, $end_date])
->get();
dd($getinfo);
}

if you want to select between dates of the media table for example, then fully qualify the field e.g.
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('media.created_at', [$start_date, $end_date]);
->get();
Also make sure the dates passed in the request are in a correct YYYY-mm-dd format

Related

Concatenate 2 JSON arrays in PHP when elements loading dynamically from a MySQL database

I'm working with a Laravel API and I have the following requirement. I'm querying the database and taking some orders. I'm checking a contract_id and I'm using 2 SQL queries to get orders with contract ID and also orders without a contract ID.
public function totalOrdersTest(Request $request)
{
$from = new DateTime($request->query->get('from'));
$to = new DateTime($request->query->get('to'));
$manufacturer_id = $request->query->get('manu');
$article_template_id = $request->query->get('article_template_id');
if ($article_template_id == '' || $article_template_id == null) {
$results_not_null = Order::selectRaw('count(*) not_null_order_count, DATE_FORMAT(orders.created_at, "%Y-%m-%d") as order_date')
->filterManufacturer($manufacturer_id)
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->whereNotNull('order_items.tariff_id')
->whereBetween('orders.created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
$results_null = Order::selectRaw('count(*) null_order_count, DATE_FORMAT(orders.created_at, "%Y-%m-%d") as order_date')
->filterManufacturer($manufacturer_id)
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->whereNull('order_items.tariff_id')
->whereBetween('orders.created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
} else {
$result = Order::selectRaw('count(*) order_count, DATE_FORMAT(created_at, "%Y-%m-%d") as order_date')
->filterArticleTemplate($article_template_id)
->whereBetween('created_at', [$from, $to])
->groupBy('order_date')
->orderBy('order_date')
->get();
}
// Need to concatenate ´$results_not_null´ & ´$results_null´ and take a one JSON array.
}
From this code, I have this JSON array with 2 objects.
{"results_not_null":[{"not_null_order_count":1,"order_date":"2020-06-09"},{"not_null_order_count":2,"order_date":"2020-06-19"},{"not_null_order_count":1,"order_date":"2020-06-29"},{"not_null_order_count":5,"order_date":"2020-07-06"},{"not_null_order_count":2,"order_date":"2020-07-07"},{"not_null_order_count":1,"order_date":"2020-07-08"},{"not_null_order_count":1,"order_date":"2020-07-15"},{"not_null_order_count":1,"order_date":"2020-07-17"},{"not_null_order_count":2,"order_date":"2020-07-22"},{"not_null_order_count":1,"order_date":"2020-07-31"},{"not_null_order_count":1,"order_date":"2020-08-01"},{"not_null_order_count":1,"order_date":"2020-08-03"},{"not_null_order_count":1,"order_date":"2020-08-15"},{"not_null_order_count":2,"order_date":"2020-08-27"},{"not_null_order_count":1,"order_date":"2020-09-15"},{"not_null_order_count":1,"order_date":"2020-09-17"},{"not_null_order_count":4,"order_date":"2020-09-18"},{"not_null_order_count":2,"order_date":"2020-09-19"},{"not_null_order_count":1,"order_date":"2020-09-20"},{"not_null_order_count":1,"order_date":"2020-09-21"},{"not_null_order_count":2,"order_date":"2020-09-22"},{"not_null_order_count":5,"order_date":"2020-09-23"},{"not_null_order_count":1,"order_date":"2020-09-24"},{"not_null_order_count":2,"order_date":"2020-09-25"},{"not_null_order_count":1,"order_date":"2020-09-26"},{"not_null_order_count":5,"order_date":"2020-09-27"},{"not_null_order_count":5,"order_date":"2020-09-28"},{"not_null_order_count":5,"order_date":"2020-09-29"},{"not_null_order_count":3,"order_date":"2020-09-30"},{"not_null_order_count":4,"order_date":"2020-10-01"},{"not_null_order_count":3,"order_date":"2020-10-02"},{"not_null_order_count":4,"order_date":"2020-10-04"},{"not_null_order_count":1,"order_date":"2020-10-05"},{"not_null_order_count":3,"order_date":"2020-10-06"},{"not_null_order_count":2,"order_date":"2020-10-07"},{"not_null_order_count":1,"order_date":"2020-10-08"}],"results_null":[{"null_order_count":1,"order_date":"2020-06-09"},{"null_order_count":1,"order_date":"2020-06-15"},{"null_order_count":1,"order_date":"2020-06-18"},{"null_order_count":3,"order_date":"2020-06-19"},{"null_order_count":1,"order_date":"2020-06-23"},{"null_order_count":1,"order_date":"2020-06-25"},{"null_order_count":1,"order_date":"2020-06-29"},{"null_order_count":21,"order_date":"2020-07-06"},{"null_order_count":2,"order_date":"2020-07-07"},{"null_order_count":3,"order_date":"2020-07-08"},{"null_order_count":1,"order_date":"2020-07-15"},{"null_order_count":3,"order_date":"2020-07-17"},{"null_order_count":3,"order_date":"2020-07-19"},{"null_order_count":2,"order_date":"2020-07-22"},{"null_order_count":3,"order_date":"2020-07-24"},{"null_order_count":1,"order_date":"2020-07-31"},{"null_order_count":1,"order_date":"2020-08-01"},{"null_order_count":1,"order_date":"2020-08-03"},{"null_order_count":1,"order_date":"2020-08-04"},{"null_order_count":1,"order_date":"2020-08-15"},{"null_order_count":5,"order_date":"2020-08-27"},{"null_order_count":2,"order_date":"2020-09-02"},{"null_order_count":2,"order_date":"2020-09-13"},{"null_order_count":1,"order_date":"2020-09-15"},{"null_order_count":1,"order_date":"2020-09-17"},{"null_order_count":4,"order_date":"2020-10-01"}]}
But I need to concatenate these 2 JSON arrays. The most important task it in both arrays, there are same date and then I need to use them in one element. For example,
[
{
"order_date":"2020-12-12",
"not_null_order_count": 2,
"null_order_count":3
},
{
"order_date":"2020-12-13",
"not_null_order_count": 12,
"null_order_count":5
},
{
"order_date":"2020-12-14",
"null_order_count":9
}
]
This is the output I need to have. Sometimes one date has only null_order_count or not_null_order_count.
Is there any method we can do this using one MySQL query? Or how to combine these 2 arrays and make one JSON array? Please advice.
try to merge both jsons:
\json_encode(array_merge(\json_decode($json1, true), \json_decode($json2, true)))

Sum of many column in Laravel

I have this function that help me to find sum of two rows and get output of total cost So again I want to get sum of the output(total cost).
public function getUsageData(Request $request)
{
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$particulars =DB::table('particulars')
->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id')
->whereBetween('date_issued', [$start_date, $end_date])
->select('particulars.item_name','particulars.unit','particulars.price','reqs.quantity_issued',
DB::raw('sum(particulars.price*reqs.quantity_issued)AS total_cost'))
->groupBy('particulars.particular_id')
->get();
}
I get sum of two row that form Total cost. I want to get sum of total cost column at the bottom so as to make it easy to understand for user without calculating it.
Try with this code else share the table structure please
$particulars =DB::table('particulars as A')
->leftjoin('reqs as B', function ($join) {
$join->on('A.particular_id', '=', 'B.particular_id');
})
->whereBetween('A.date_issued', [$start_date, $end_date]) // i have taken date issued from table particulars as you have not mentioned else you can change A.date_issued as B.date_issued .
->select('A.item_name','A.unit','A.price','B.quantity_issued',DB::raw('sum(A.price*B.quantity_issued) as total_cost'))
->groupBy('A.particular_id')
->get();

Intval function in Laravel eloquent query

I am doing filter script. User write age_to and I get users with maximum age. The problem is that my in my users table is only full date of birth. How can I get all data? I think intval function is good but I do not know how to use it in mysql query.
$user = User::with('user_x')->whereHas('user_x', function($query) use ($request) {
$birth_date = Carbon::now()->subYears($request->age_to)->toDateString();
return $query->where('date_of_birth', '<=', $birth_date);
})->get();
Intval probably won't do what you want, but Carbon may. Since you're going for age, then use Carbon's date setters:
$birth_date = Carbon::now()->subYears($request->age_from)->toDateString(); // Sets the birth date to today's date minus the requested years
Then you can use it in your query:
$user = User::with('user_x')->whereHas('user_x', function($query) use ($birth_date) {
return $query->where('date_of_birth', '<=',$birth_date);
})->get();

Laravel date format in where condition

I have this code
use Carbon;
$now = Carbon::now()->toDateString();
So i get the date without the time
public funtion test($brandid){
$nbofsale=DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodate', $now)
->get();
return $nbofsale;
}
The eodate field in the select is datetime. I want to put it in this query as date only and not to change the field type in database.
Any ideas?
If eodate is a timestamp, you don't need to convert it to a date string:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->whereDate('eodata', Carbon::today()->toDateString());
->get();
Another way to get record for today:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodata', '>', Carbon::now()->startOfDay());
->get();

Query the DB DATETIME column just by date

I have a column in the DB with DATETIME type and is with format yyyy-mm-dd hh:mm:ss. Nevertheless, as information I am given just the date (and I don't care about the time). So how should I query the DB to search that column just by date, even though the value contains both the date and the time?
Update:
Here's the PHP code where I am trying to select the entities:
public static function getTripsWithDrivers($route_from = null, $route_to = null, $date = null)
{
$trips = Trip::whereRouteFrom($route_from)->whereRouteTo($route_to)->whereStartDate($date);
...
}
Use MySql date function:
$trips = Trip::where(DB::raw('DATE(route_from)'), '=', $route_from)
->where(DB::raw('DATE(route_to)'), '=', $route_to)
->where(DB::raw('DATE(start_date)'), '=', $date)
User where filter as the following:
WHERE "datetime column" like '2015-04-06%'

Categories