cast datetime to date - laravel-5

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();

Related

How to get data which has expiry date nearest to current date?

I have PurchaseOrderProduct Model from which I am getting data like that
PurchaseOrderProduct::with('products')
->with('warehouse_locations')
->with('productStatuses')
->with('purchase_orders')
->where('product_id', $request->product_id)
->where('free_qty', '>=', 1)
->get();
Now in this table, I have the column expiry_date of type date i want to fetch data orderBy expiry_date nearest to the current date .
Means purchase_order_product whose expiry date is nearest to current date should come first.
$product = PurchaseOrderProduct::with('products')
->with('warehouse_locations')
->with('productStatuses')
->with('purchase_orders')
->where('product_id', $request->product_id)
->where('free_qty', '>=', 1)
->whereDate('expiry_date', '>', Carbon::now())
->orderBy('expiry_date','asc')
->get();

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();

query created_at date only equals to carbon::now() date only in 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

How can i show single data in view if today date is in between start date & end date in laravel?

I have Admission table which have id, school_id, start_date,end_date etc and i want to show single data if today's date is between start_date & end_Date in laravel eloquent
Applying below code you can get the latest data of current date
$currentDate = date('Y-m-d');
$result = my_table::where(DB::raw('DATE(start_date)'), '<=' , $currentDate)
->where(DB::raw('DATE(end_Date)'), '>' , $currentDate)
->orderBy('id','desc')
->first();
`$start_date = date('2018-01-01');
$end_date = date('2018-05-02');
Adminssion::whereBetween(Carbon\Carbon::now()->toDateString(), [$start_date, $end_date])->get();`
The carbon now() function will give you current date and time. And toDateString() function only give you today date. And with whereBetween() function you can compare the current date between start_date and end_date.

convert raw query in laravel eloquent way

How to convert this query in Laravel eloquent way?
SELECT * FROM posts WHERE status=1 and now() BETWEEN start_time and end_time ORDER BY id DESC LIMIT 1
// What so far i have tried
Post::whereStatus(1)->orderBy('id','desc')->get()
"and now() BETWEEN start_time and end_time" .I am unable to convert
this part in eloquent way.
Any suggestion would be highly appreciated.
You can do the following
Post::where('status', 1)
->where('start_time', '>', Carbon::now())
->where('end_time', '<', Carbon::now())
->orderBy('id', 'DESC')
->take(1)
->get();
You can achieve the same semantic by defining that your start_time is lower than the current datetime and your end_time is greater than it:
Post::whereStatus(1)
->where("start_time", "<", Carbon::now())
->where("end_time", ">", Carbon::now())
->orderBy('id','desc')
->first();

Resources