Laravel carbon check if past date from table - php-carbon

So i want to get data from my API where datetime in the past,but when i tried this code.. its appear that i got data where ONLY DATE in the past not include the time
$id_merchant = $request->input('id_merchant');
$Promotion = DB::table('promotion')
->leftJoin('product', 'product.id', '=', 'promotion.id_product')
->where('promotion.id_merchant', '=', $id_merchant)
->whereDate('end_time','>=', Carbon::now())
->select('promotion.*','product.product_name','product.price','product.stock')
->get();
i see some people solve it with
$end_time->isPast() function
but i dont know how to implement this in my case.
sorry for my english

->where('end_time', '>=', Carbon::now())
(whereDate filter the time)

Related

How to convert a table column data inside eloquent in laravel?

I am trying to convert the 'CHECKTIME' table column datetime to string for using the 'LIKE' operator in sql. The LIKE operator is not support the datetaime data type. So, I have to convert it into string to use LIKE. How can I do it in laravel eloquent? My Code in Controller:
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->where(CONVERT(VARCHAR(25),' CHECKINOUT.CHECKTIME'),'LIKE',$thisDay.'%')
->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
The SQL Query that work to return the data:
SELECT CHECKINOUT.CHECKTIME
FROM CHECKINOUT
JOIN USERINFO ON USERINFO.USERID=CHECKINOUT.USERID
WHERE CONVERT(VARCHAR(25), CHECKINOUT.CHECKTIME, 126) LIKE '2021-11-23%';
First off, you can use the whereRaw() (see link) method to inject raw SQL in your query.
This would be something like this:
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->whereRaw('CONVERT(VARCHAR(25), CHECKINOUT.CHECKTIME) LIKE ?', $thisDay.'%')
->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
But since you are working with dates, it's recomended to use dates all the way and not convert the data to compare strings.
Another approach would be to use whereBetween directly with dates. (see link)
Exemple :
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->whereBetween(
'CHECKINOUT.CHECKTIME',
[
$thisDay,
date('Y-m-d', strtotime('+1 day', $thisDay))
]
)->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
I have over indented the relevent section to make it clear.
Reach out in comments if you have any issue or suggestion to improve it :)

fetch data using query builder by checking multiple data from same column in laravel

DB::table('products')
->where('status', '=', 'published')
->where('sub_category', '=', 'grooming-wellness')
->where('sub_category', '=', 'beauty-care')
->get();
it doesn't work. it returns 0 data.
I am assuming you want to check if a field has a particular value or another value. You can use whereIn to achieve this:
DB::table('products')
->where('status', '=', 'published')
->whereIn('sub_category', ['grooming-wellness', 'beauty-care'])
->get();
Laravel 6.x Docs - Query Builder - Where Clauses - Additional Where Clauses whereIn

Laravel Elequent Count from Multiple Columns

I'm trying to get the count of 2 columns('published', 'expires') in Laravel 5.6.
I want my results count to only show if the item is published, not expired or there is no expiration date.
my code:
$today = date('Y-m-d');
$numrecords = deals::where('published',1)
->orwhereDate('expires', '=', '')
->orwhereDate('expires', '>=', $today)
->count();
thanks
Nabi
$numrecords = deals::where('published', 1)
->where(function($q){
return $q->whereNull('expires')
->orwhereDate('expires', '>=', Carbon::today());
})
->count();
Above should give you :
SELECT * FROM deals
WHERE
published = 1 AND
( expires IS NULL OR DATE(expires) >= today_date)
Here are the queries you are supposed to write: (today is a Laravel function)
// 1. if the item is published
$item->where('published', true);
// 2. expire date is after today
$item->where('expiry_date', '>', today());
// 3. expire date is null
$item->whereNull('expiry_date');
//4. emit expired items
$item->where('expiry_date', '>', today());
I think some of your check conditions are wrong.
Also, your logic is very messy as well. I am not sure what condition you are trying to write, but do write it down, under what condition, is an item considered into the count, then you decide to use where or orWhere.

Laravel Eloquent with `has` and `where` on dates

I try to get records on relation when there is only one record in the one to many relation and only the records where the start_date is bigger than now() and here is what I'm trying:
$newStarters = User::has('periods', 1)->with(['periods' => function($q) {
$q->where('start_date', '>', Carbon::now()->subWeek(2)->format('Y-m-d') );
}])->get();
in this case the date filter does not applies.
What is the propper way to do this?
Probably you are looking this:
$newStarters = User::whereHas('periods', function($q) {
$q->where('start_date', '>', Carbon::now()->subWeek(2)->format('Y-m-d') );
}, '=', 1)->get();
See Query relationship existence section for more details.

Laravel list issue

I have below query which is directly linked to drop down. However, if I lists() with join() it gives
errors: ambiguous 'id'
Code:
Company::join('users', 'users.company_id', '=', 'companies.id')
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->lists('name', 'id');
What could be a solution with lists()? any help would be real help.
Both tables contain an id field. The solution here would be to prepend the table name to the id field that you want to list. For example:
Company::join('users', 'users.company_id', '=', 'companies.id')
->select(
'companies.name as companyName',
'companies.id as companyID'
)
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->lists('companyName', 'companyID');
The error showing because you id column in both of your table. I don't know which table's id you needed. In this answer I assume you don't need the id of user so I declared it as uid. So, there is only one column named id which is belong to companies table.
Company::join('users', 'users.company_id', '=', 'companies.id')
->distinct()
->where('companies.name', 'NOT LIKE', '%XXXX%')
->select('users.id as uid','companies.id','companies.name')
->lists('name', 'id');
Try this code:
DB::table('companies')->distinct()->join('users',function($join){
$join->on( 'users.company_id', '=', 'companies.id')
->where('companies.name', 'NOT LIKE', '% %');
}) ->lists('companies.name');
P.S. works fine for me