difference between 2 dates - illuminate manager laravel - laravel

I make a tv program schedule and I need to sort dates that are more than 600 seconds apart
But it don't work ;(
Anyone know how to do it?
Many thanks in advance to those who will help me.
$dateMin = Carbon::now('Europe/Paris')
->endOfDay()
->addDay()
->addHours(-4)
->timestamp;
$dateMax = Carbon::now('Europe/Paris')
->endOfDay()
->addDay()
->timestamp;
$datas = Capsule::table('channels')
->select('channels.number',
'channels.slug',
'channels.display-name',
'channels.icon',
'programs.start',
'programs.stop',
'programs.title',
'programs.img',
'programs.thumbnail',
'programs.desc'
)
->where([
['start', '>', $dateMin],
['stop', '>', $dateMin],
['start', '<', $dateMax],
])
->whereRaw('stop - start > 601')
->leftJoin('programs', 'channels.slug', '=', 'programs.slug')
->orderBy('number')
->orderBy('start')
->get();
return $datas->groupBy('display-name');

Without much context, the only thing I can think of is to try grouping the parameters in where clause
$datas = Capsule::table('channels')
->select('channels.number',
'channels.slug',
'channels.display-name',
'channels.icon',
'programs.start',
'programs.stop',
'programs.title',
'programs.img',
'programs.thumbnail',
'programs.desc'
)
->where(function($query) use($dateMin,$dateMax) {
$query->where([
['start', '>', $dateMin],
['stop', '>', $dateMin],
['start', '<', $dateMax],
])
->whereRaw('stop - start > 601')
})
->leftJoin('programs', 'channels.slug', '=', 'programs.slug')
->orderBy('number')
->orderBy('start')
->get();
If this doesn't work, try dumping the query sql for both, without parameter grouping (code in your question) and with parameter grouping (code above in my answer) and see the sql statement generated. You can get the query sql using toSql() in place of get().

Related

case when not working in laravel query joint script

$billing_review_data = DB::table('_bvARAccountsFull As cl')
->whereIn('DCLink',$clients)
->whereYear('ReadingDate', '=', $today->year)
->whereMonth('ReadingDate', '=', $today->month)
->select('cl.name','cl.account','cl.currencycode','cl.ulARJointSeparateBill','sa.ucSABillingAsset',DB::raw('sum(cmr.moncmr - cmr.monpmr) as monunit'),
DB::raw('sum(cmr.colcmr - cmr.colpmr) as colunit'),
DB::raw('sum(cmr.scncmr - cmr.scnpmr) as scnunit'),
DB::raw('sum(cmr.a3mcmr - cmr.a3mpmr) as a3munit'),
DB::raw('sum(cmr.a3ccmr - cmr.a3cpmr) as a3cunit'),
DB::raw('(CASE WHEN sa.ucSABillingAsset IS NULL THEN sa.ccode ELSE sa.ucSABillingAsset END) AS billingasset'),
DB::raw("(CASE WHEN sa.ucSABillingAsset IS Null THEN sa.cdescription ELSE 'Billed Asset' END) AS assetdesc")
)
->join('_smtblServiceAsset As sa', 'cl.DCLink', '=', 'sa.icustomerid')
->join('_cplmeterreading As cmr', 'sa.autoidx', '=', 'cmr.assetid')
->groupBy('cl.name','cl.account','cl.currencycode','cl.ulARJointSeparateBill','sa.ucSABillingAsset')
->get();
dd( $billing_review_data);
am using the CASE WHEN IS IN MY QUERY BUT IT DOESNT WORK AS INTENDED WHAT COULD BE THE ISSUE

Count every records that exist on each program per college using Laravel

I have this query that produces all the College program.
$report = DB::table('college')
->leftJoin('program', 'program.college_id', '=', 'college.college_id')
->leftJoin('application', 'application.college_id', 'college.college_id')
->leftJoin('application_status', 'application_status.id', '=', 'application.status')
->select('college.college_name', 'program.program_name', 'program.program_id', 'program.track', 'application.acad_year', 'application.semester', 'application_status.name as name',
// DB::raw('SUM(application.application_no) as total'),
DB::raw('COUNT(program.college_id = '.$request->college_id.') as total'),
)
->where([
'application.acad_year' => $request->acad_year,
'application.semester' => $request->semester,
'college.college_id' => $request->college_id,
'application.status' => $request->status_id
])
->groupBy('program.program_id')
->get();
What I want is, I want to get all the "Applicants" each "Program" by Colleges. My problem is, the count of all applicants on each programs by colleges.

How to Only fetch time from timestamp in laravel

I am working on a Laravel project. I am trying to fetch time from a timestamp. This is the code I'm using to try to accomplish this:
$details=DB::table('CourseListNew as cl')
->select(
'cl.Title',
'p.Title as ParentTitle',
'cd.Trainer',
DB::raw('day(cd.StartingDate) as day'),
DB::raw('day(cd.EndingDate) as day_end'),
DB::raw('(cd.StartingDate) as month'),
DB::raw('year(cd.StartingDate) as year'),
DB::raw('time(cd.StartingDate) as start_time'),
DB::raw('time(cd.EndingDate) as end_time'),
'cd.StartingDate',
'cd.EndingDate',
'cd.Duration',
'cd.Type',
'cd.Fee',
'cd.Venue',
'count (s.Id) as TotalRegistartion'
)
->join('CourseListNew as p','p.Id', '=', 'cl.ParentId','left')
->join('CourseDetailsNew as cd','cd.CourseId', '=', 'cl.Id')
->join('Student as s','s.CourseId', '=', 'cl.Id', 'left outer')
->orderBy('cl.Id','DESC')
->get();
i want to get time in 12 hours format with AM/PM but not able to find any working solution. i am not using eloquent.
Assuming you are using MySQL, you can replace the TIME function with DATE_FORMAT
$details=DB::table('CourseListNew as cl')
->select(
'cl.Title',
'p.Title as ParentTitle',
'cd.Trainer',
DB::raw('day(cd.StartingDate) as day'),
DB::raw('day(cd.EndingDate) as day_end'),
DB::raw('(cd.StartingDate) as month'),
DB::raw('year(cd.StartingDate) as year'),
DB::raw('DATE_FORMAT(cd.StartingDate, '%r') as start_time'),
DB::raw('DATE_FORMAT(cd.EndingDate, '%r') as end_time'),
'cd.StartingDate',
'cd.EndingDate',
'cd.Duration',
'cd.Type',
'cd.Fee',
'cd.Venue',
'count (s.Id) as TotalRegistartion'
)
->join('CourseListNew as p','p.Id', '=', 'cl.ParentId','left')
->join('CourseDetailsNew as cd','cd.CourseId', '=', 'cl.Id')
->join('Student as s','s.CourseId', '=', 'cl.Id', 'left outer')
->orderBy('cl.Id','DESC')
->get();
The %r format corresponds to the 12-hour time followed by AM or PM.
For more details read the documentation on: DATE_FORMAT
In SQL Server the equivalent method would be FORMAT(time, N'hh:mm tt') and you might need to refer to your own DBMS documentation for the equivalent in other flavours of SQL
For a Laravel generic solution you can of course just format the date after you've gotten the data using Carbon
use Mysql DATE_FORMAT
DB::raw('DATE_FORMAT(cd.EndingDate,'%Y-%m-%d %h:%i %p') as end_time')

Laravel 5 where condition to get pass 30 days worth of records

I have a date column in the instances table with varchar datatype stored as d-m-Y. In my where condition I am trying to fetch records for just past 30 days only.
$backdate = Carbon::parse('-30 days')->toDateString();
$date30DaysBack = Carbon::parse($backdate)->format('d-m-Y');
$adverts = DB::table('adverts')
->where(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'), '>=',$date30DaysBack)
The full query
$adverts = DB::table('adverts')
->select(DB::raw('(SELECT IF(ext = \'jpeg\', CONCAT(fullpath, \'_1.\', ext), (CONCAT(fullpath,\'.\',ext))) as fullpath FROM advertsstorage where uid_dir = adverts.ad_uid ORDER BY id ASC limit 1)as fullpath, adverts.*, domains.location,instances.date'))
->join('domains','adverts.domain', '=' ,'domains.domain')
->join('advertiser_domains','domains.id', '=' ,'advertiser_domains.domain_id')
->join('advertisers','advertiser_domains.advertiser_id', '=' ,'advertisers.u_id')
->join('instances','adverts.ad_uid','=','instances.ad_uid')
->join('urls','instances.u_id','=','urls.id')
->join('sites','urls.sites_id','=','sites.id')
->where('advertisers.u_id', '=',$advertiserID)
->where(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'), '>=',Carbon::now()->subDays(30)->format('d-m-Y'))
->orderBy(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'), 'DESC')
->get();
Did you try DATEDIFF method
$adverts = DB::table('adverts')
->select(DB::raw('(SELECT IF(ext = \'jpeg\', CONCAT(fullpath, \'_1.\', ext), (CONCAT(fullpath,\'.\',ext))) as fullpath FROM advertsstorage where uid_dir = adverts.ad_uid ORDER BY id ASC limit 1)as fullpath, adverts.*, domains.location,instances.date'))
->join('domains','adverts.domain', '=' ,'domains.domain')
->join('advertiser_domains','domains.id', '=' ,'advertiser_domains.domain_id')
->join('advertisers','advertiser_domains.advertiser_id', '=' ,'advertisers.u_id')
->join('instances','adverts.ad_uid','=','instances.ad_uid')
->join('urls','instances.u_id','=','urls.id')
->join('sites','urls.sites_id','=','sites.id')
->where('advertisers.u_id', '=',$advertiserID)
->where(DB::raw('DATEDIFF( now(), STR_TO_DATE(instances.date,"%d-%m-%Y") )'), '<=', 30)
->orderBy(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'), 'DESC')
->get();
Have you checked out the Carbon docs?
You can use the subtraction modifier for this; Carbon::now()->subDays(30);.
Would look something like this in your code:
$adverts = DB::table('adverts')->where(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'), '>=', Carbon::now()->subDays(30)->format('d-m-Y'))
Try this.
Carbon has subDays function by which you can get past 30 days data
$date30DaysBack= Carbon::now()->subDays(30)->format('d-m-Y');
$today = Carbon::now()->format('d-m-Y');
$adverts = DB::table('adverts')
->whereBetween(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'),[$date30DaysBack,$today])->get()

Memory size error in laravel pagination

When I try to select some field with join query with pagination it show en error like
Allowed memory size of 134217728 bytes exhausted (tried to allocate 45797376 bytes)
My query is
$price_all = DB::table('model_price')
->join('operator_model','model_price.model_id','=','operator_model.id')
->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
->join('route', 'operator_route.route_id', '=', 'route.id')
->join('operator', 'operator_route.operator_id', '=', 'operator.id')
->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
->paginate(2);
In my database only 5 records are stored. it not big data.
when I try without pagination then it works fine. like
$price_all = DB::table('model_price')
->join('operator_model','model_price.model_id','=','operator_model.id')
->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
->join('route', 'operator_route.route_id', '=', 'route.id')
->join('operator', 'operator_route.operator_id', '=', 'operator.id')
->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
->get();
Now how can I optimize this query.
Try using Offset & Limit instead of paginate.
$price_all = DB::table('model_price')
->join('operator_model','model_price.model_id','=','operator_model.id')
->join('operator_route','operator_model.operator_route_id','=','operator_route.id')
->join('route', 'operator_route.route_id', '=', 'route.id')
->join('operator', 'operator_route.operator_id', '=', 'operator.id')
->select('model_price.id', 'model_price.price', 'route.route_name', 'operator.operator_name')
->skip(0)
->take(2)
->get();

Resources