query created_at date only equals to carbon::now() date only in laravel - laravel

How to do you query created_at date only equals to Carbon::now() date only in laravel query builder.?
App\Table::where('created_at', '=' ,Carbon/Carbon:now() )->get();

You can opt for the following kind of construct:
App\Table::whereDate('created_at', '=', now()->toDateString())->get();

You can use Mysql's default CURDATE function with laravel query builder's Raw sql.
DB::table('posts')->select(DB::raw('*'))
->whereRaw('Date(created_at) = CURDATE()')->get();
Or
App\Table::where('created_at', '=', Carbon::today())

No Need to use anything just check whereDate For Date
App\Table::whereDate('created_at', '=' ,Carbon/Carbon:now() )->get();
among this whereMonth for Month and whereDay for date
check this link https://laravel.com/docs/5.8/queries#whereDate%20/%20whereMonth%20/%20whereDay%20/%20whereYear%20/%20whereTime

Related

Fetch records that matches month and year using Laravel eloquent

How do I query a table such that it only fetches records that matches a particular month and year e.g. Y-m i.e. 2020-10. of the 'created_at' column.
This is what I am currently doing:
School::where('user_id', 1)->where('created_at', '=', '2020-10')->count();
I would suggest to create a date range for your filter so this way your query will remain sargable
$date = \Carbon\Carbon::parse($year."-".$month."-01");
$start = $date->startOfMonth()->format('Y-m-d H:i:s');
$end = $date->endOfMonth()->format('Y-m-d H:i:s');
School::where('user_id', 1)
->whereBetween('created_at', [$start, $end])
->count();

Sorting data order by duration between start_date and end_date

I'm using Laravel 7. I have a table named assistances witch has some columns and those two columns:
- start_date
- end_date
I'd like to sort data order by the duration between start_date and end_date:
$query = Assistance::join('users', 'assistances.user_id', '=', 'users.id')
->select('assistances.*')
->orderBy('duration between start_date and end_date');
;
How can I do this ?
Please try the following - bare in mind that this is not hugely efficient
DATEDIFF returns the days between two dates. You may also want to use TIMESTAMPDIFF which accepts a first parameter such as 'hour' where you can get more granular control
$query = Assistance::join('users', 'assistances.user_id', '=', 'users.id')
->select('assistances.*')
->orderBy(DB::raw('DATEDIFF(start_date, end_date)'))
->get();
Please also use DB; in your class.

how to convert query php to laravel framework

how can I convert this query to Laravel query
SELECT * FROM dbx_a
WHERE date BETWEEN NOW() - INTERVAL 30 DAY AND NOW() AND name = 'MANAGEMENT'
ORDER BY date DESC
you can use carbon to help you building your query:
$beforeThirtyDay = Carbon::now()->subDays(30);
DB::table('dbx_a')->select('*')->whereBetween('date', array(Carbon::now(), $beforeThirtyDay))
->where('name', '=', 'MANAGEMENT')->orderByDesc('date')->get();
Please try this:
DB::table('dbx_a')
->whereRaw('date BETWEEN NOW() - INTERVAL 30 DAY AND NOW()')
->where('name', 'MANAGEMENT')
->orderByDesc('date')
->get();

cast datetime to date

I'm doing a whereBetween dates query on Eloquent. However, I have an issue with the date time.
If I select date_from to 2018-11-01 and date_to to 2018-11-29
and if I have a data that was created on 2018-11-29 12:28:45 the data created on this datetime will not be included because of the time.
Is there a way to cast the created_at to date?
here's my code:
Payable::with('details', 'transaction_type')
->whereBetween('created_at', [$request->date_from, $request->date_to])
->get();
You can set time to your date variables.
$request->date_from = $request->date_from . ' 00:00:00';
$request->date_to = $request->date_to . ' 23:59:59';
I feel a much cleaner way to handle dates in Laravel is using the Carbon package.
$date_from = Carbon::createFromFormat('Y-m-d', $request->date_from)->startOfDay();
$date_to = Carbon::createFromFormat('Y-m-d', $request->date_to)->endOfDay();
Just FYI, created_at column when accessed via eloquent will return a carbon date object by default.
Hope this helps.
K
If you don't mind adding an extra -> to your querying, you can very easily do this:
Payable::with('details', 'transaction_type')
->whereDate('created_at', '>=', $request->date_from)
->whereDate('created_at', '<=', $request->date_to)
->get();

Laravel join two tables with where that compares two date time columns

I have two tables that I want to run joint query on using the value of two date time columns, I have products table and sync_status tables, I want to return all products with updated_at date time greater than synced_at date time.
DB::table('products')
->join('sync_statuses', function ($join) {
$join->on('products.product_number', '=', 'sync_statuses.product_number')
->where('products.updated_at', '>', 'sync_statuses.synced_at')
->whereNull('products.deleted_at');
})
->select('products.product_number');
This SQL represents what I am trying to achieve using Eloquent:
SELECT products.product_number
FROM products
JOIN push_statuses
ON products.product_number = statuses.product_number
AND (
statuses.synced_at IS NULL
OR products.updated_at > statuses.synced_at
)
You have to use on() instead of where() to compare columns:
->on('products.updated_at', '>', 'sync_statuses.synced_at')
This worked for me:
DB::table('products')
->join('statuses', function ($join) {
$join->on('products.product_number', '=', 'statuses.product_number')
->where(DB::raw('products.updated_at'), '>=', DB::raw('statuses.synced_at'))
->whereNull('products.deleted_at');
})->select('products.product_number');
I needed to use DB::raw('products.updated_at') to reference each date time column in the where() clause.

Resources