Get the date expired in 60 day using Laravel - laravel

I'm trying to get the date_fin_contrat which will be expired in 60 days.
So I'm using the following formula:
date_fin_contrat-60 <= current_date
So in Laravel I'm using the following code:
$contrats_en_cours = Contrat::where('contrats.actif', 1)
->join('projets_castings', 'contrats.id_contrat', '=', 'projets_castings.id_contrat')
->leftjoin('projets', 'projets.id_projet', '=', 'projets_castings.id_projet')
->leftjoin('castings', 'contrats.id_casting', 'castings.id_casting')
->whereDate('contrats.date_fin_contrat', '-', 60, '<=', Carbon::today())
->get();
For example: Current date is 09-08-2021 and date_fin_contrat is 09-10-2021 I should get this row.
But I get nothing, is there something wrong with my query?
If you have any idea please help.

I haven't used laravel whereDate function but you can do it this way simply
date_fin_contract = "09-10-2021"
$today_date = Carbon::now()->addDays(60)->toDateTimeString();
in where clause you can simpley check
where('date_fin_contract', '<=', $today_date);
<= represent contract is not expired
>= reprsent contract is expired

Related

Laravel date time condition not working properly

I have two columns in my offer table as follow, I want to fetch those offer's record which is currently active:
offer_start_date
offer_end_date
24-10-2022 10:57:00
24-10-2023 10:57:00
Below is my query date condition working fine but time is giving problems:
$now = Carbon::now();
$today = $now->toDateString();
$currentTime = $now->toTimeString();
$offers_data = Offer::whereRaw("offer_status=2 and offer_type=1 and is_special_offer=3")
->whereDate('offer_start_date','<=',$today)
->whereTime('offer_start_date','<=',$currentTime)
->whereDate('offer_end_date','>=',$today)
->whereTime('offer_end_date','>=',$currentTime)
->limit(10)
->get();
What would be the reason?
Using whereDate and whereTime together is likely not working the way you think it is.
Instead, try just using where() and Laravel will work it out for you.
$now = Carbon::now();
$offers_data = Offer::whereRaw("offer_status=2 and offer_type=1 and is_special_offer=3")
->where('offer_start_date', '<=', $now)
->where('offer_end_date', '>=', $now)
->limit(10)
->get();

How to get created column using SelectRaw and use it inside whereBetween

I've got a laravel eloquent query which uses selectRaw and whereBetween:
$offset = '+8:00';
$d["records"] = data_entries::select("*")
->selectRaw("CONVERT_TZ (testTime, '+0:00', '{$offset}') as convertedTime")
->whereBetween("testTime", $filters['duration'])
->where('data_entries.patientID_FK', '=', $patient['patientID'])
->leftjoin('additional_notes', 'ID', '=', 'additional_notes.entryID_FK')
->get();
So this query works and I can get convertedTime data. But I want to use convertedTime instead of testTime in whereBetween like this:
$d["records"] = data_entries::select("*")
->selectRaw("CONVERT_TZ (testTime, '+0:00', '{$offset}') as convertedTime")
->whereBetween("convertedTime", $filters['duration'])
->where('data_entries.patientID_FK', '=', $patient['patientID'])
->leftjoin('additional_notes', 'ID', '=', 'additional_notes.entryID_FK')
->get();
but I got error saying unknown column 'convertedTime'. Is there a way to get the date that was converted based on timezone inside the whereBetween?
You cannot refer to an alias defined in the select clause in the where clause at the same level of the query. But it just so happens that MySQL has overloaded the HAVING operator such that it can be used in place of WHERE, with the added feature that it can also refer to aliases. Consider the following version:
$d["records"] = data_entries::select("*")
->selectRaw("CONVERT_TZ (testTime, '+0:00', '{$offset}') AS convertedTime")
->where('data_entries.patientID_FK', '=', $patient['patientID'])
->leftjoin('additional_notes', 'ID', '=', 'additional_notes.entryID_FK')
->havingRaw("convertedTime >= ? AND convertedTime <= ?", $filters['duration'])
->get();

How can I write Laravel eloquent query (inner query) for this plain SQL query

SELECT *
FROM (
SELECT *
FROM appointments
WHERE `date` <= Curdate()
) `t`
WHERE `t`.`end_time` <= current_time
How can I write a Laravel eloquent equivalent query for this SQL query?
If you want to query:
all past appointments (from start to yesterday, based on date field) +
all today appointments that have end_time not past (or exactly now) for today
And assuming you have a proper Model for the appointment table you could try something like :
Appointments::where('appointments.date', '<', Carbon::now()->toDateString())
->orWhere(function ($query) {
$query->where('appointments.date', Carbon::now()->toDateString())
->where('appointments.end_time', '<=', Carbon::now()->toTimeString());
});
You may need to test the Carbon format used in where clauses to be sure it matches your database storage format, and the query return expected results.
I am not sure whether this suit you, you can try it:
Appointment::whereIn('id', function($query) {
$query->select('id')
->from('appointment')
->where('date', '<=', DB::raw('CURDATE()'));
})
->where('end_time', '<=', DB::raw('CURRENT_TIME()'));
Try this i hope you got your solution .
Appointment::whereExists(function ($query){ $query->selectRaw(1) ->from((new
Appointment())->getTable()) ->whereDate("date",'<=',now()->format('Y-m-d')); })-
>whereDate("end_time",'<=',now()->format('H:i:s'))->get()

Using day, Month or Year as MYSQL query filter in Laravel

In my code, i am querying a database by accepting month and year input from user.
I have tried writing it the normal PHP way and other ways i can find online but none seems to be working. here is the code i am using currently
$salesmonth = $request->input('mn');
$salesyear = $request->input('yr');
$id = Auth::user()->id;
$comm = \DB::table('bakerysales')
->where([
['customer_id', '=', $id], [MONTH('sales_date'), '=', $salesmonth], [YEAR('sales_date
'), '=', $salesyear]
])
->get();
return view::make('showCommission')->with('comm', $comm);
I expect the query to return data from rows that match user selected month and year
Laravel comes with a few different where clauses for dealing with dates e.g.
whereDate / whereMonth / whereDay / whereYear.
This means that your controller method can look something like:
$comm = \DB::table('bakerysales')
->where('customer_id', auth()->id())
->whereMonth('sales_date', $request->input('mn'))
->whereYear('sales_date', $request->input('yr'))
->get();
return view('showCommission', compact('comm'));

retrive records where updated_at is not older than X seconds in Laravel (eloquent)

Following the examples found here on stack overflow I'm trying to do this:
$users = User::where('updated_at', '>', time() - (5))->get();
As the title of the post, I would like to receive only the records where the field "updated_at" is not older than 5 seconds. I also tried something like that:
...where('updated_at', '<', DB::raw('(NOW(), INTERVAL 5 SECOND)'))...
or
...where('updated_at', '>=', time() - (1*1*5))->get();
but keeps giving me even the obsolete records...
This way:
$users = User::where('updated_at', '>', \Carbon\Carbon::now()->subSeconds(5)->toDateTimeString())->get();

Resources