Laravel date format in where condition - php

I have this code
use Carbon;
$now = Carbon::now()->toDateString();
So i get the date without the time
public funtion test($brandid){
$nbofsale=DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodate', $now)
->get();
return $nbofsale;
}
The eodate field in the select is datetime. I want to put it in this query as date only and not to change the field type in database.
Any ideas?

If eodate is a timestamp, you don't need to convert it to a date string:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->whereDate('eodata', Carbon::today()->toDateString());
->get();
Another way to get record for today:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodata', '>', Carbon::now()->startOfDay());
->get();

Related

Retrieve data from the database using Eloquent between two dates

I'm having trouble retrieving data from the database using Eloquent. I am trying to retrieve all data between two dates (created_at field).
I've done this:
public function search(Request $request){
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('Date_Occured',[$start_date, $end_date])
->get();
dd($getinfo);
}
if you want to select between dates of the media table for example, then fully qualify the field e.g.
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('media.created_at', [$start_date, $end_date]);
->get();
Also make sure the dates passed in the request are in a correct YYYY-mm-dd format

Intval function in Laravel eloquent query

I am doing filter script. User write age_to and I get users with maximum age. The problem is that my in my users table is only full date of birth. How can I get all data? I think intval function is good but I do not know how to use it in mysql query.
$user = User::with('user_x')->whereHas('user_x', function($query) use ($request) {
$birth_date = Carbon::now()->subYears($request->age_to)->toDateString();
return $query->where('date_of_birth', '<=', $birth_date);
})->get();
Intval probably won't do what you want, but Carbon may. Since you're going for age, then use Carbon's date setters:
$birth_date = Carbon::now()->subYears($request->age_from)->toDateString(); // Sets the birth date to today's date minus the requested years
Then you can use it in your query:
$user = User::with('user_x')->whereHas('user_x', function($query) use ($birth_date) {
return $query->where('date_of_birth', '<=',$birth_date);
})->get();

Laravel: Get all records where expires_at is in the past?

I have a variable where I get the records from GameMute modal and I need to know how I can only get the ones where expires_at is not yet expired. Its formatted just like created_at
$mutes = GameMute::orderBy('created_at', 'asc')->get();
Looked all over google but its with formatted time or something else.
Thanks.
Use Carbon like this,
$mutes = GameMute::where('expires_at','>', \Carbon\Carbon::now())
->orderBy('created_at', 'asc')->get();
Try this one :
$date = date("Y-m-d h:i:s");
$users = DB::table('users')->whereDate('expires_at',">",$date))
->orderBy('created_at', 'asc')->get();
Try where condition
$mutes = GameMute::whereRaw('expires_at > now()')->orderBy('created_at', 'asc')->get();
Try these
$mutes = GameMute::where('expires_at ', '>', new DateTime('today'))->orderBy('created_at', 'asc')->get();
Hope your expire_at column is a date type column
$mutes = GameMute::where('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();
Or if it contained date time value like "2017-07-31 16:50" then you can query like this :
$mutes = GameMute::whereDate('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();

Laravel Eloquent where with calculation on value

I have a column in my table called date which is of type TIMESTAMP.
I am trying to add a very simply scope which will return all results that have a date of tomorrow.
I think I want to be able to do something like this:
public function scopeTomorrow($query)
{
return $query->where('date', function ($date) {
return \Carbon\Carbon::parse($date)->isTomorrow();
});
}
But the $date variable is currently just the query builder.
How can I create a where statement which accepts the value and perfroms some sort of check on it?
You can do this:
return $query->where('date', '>=', Carbon::tomorrow())
->where('date', '<=', Carbon::tomorrow()->endOfDay());
Or:
return $query->whereBetween('date', [Carbon::tomorrow(), Carbon::tomorrow()->endOfDay()]);
Also, probably you want to add date to $dates variable.
You can do something like this:
public function scopeTomorrow($query)
{
$tomorrow = \Carbon\Carbon::now()->addDay(1);
return $query->where('date', '=', $tomorrow);
}

Query the DB DATETIME column just by date

I have a column in the DB with DATETIME type and is with format yyyy-mm-dd hh:mm:ss. Nevertheless, as information I am given just the date (and I don't care about the time). So how should I query the DB to search that column just by date, even though the value contains both the date and the time?
Update:
Here's the PHP code where I am trying to select the entities:
public static function getTripsWithDrivers($route_from = null, $route_to = null, $date = null)
{
$trips = Trip::whereRouteFrom($route_from)->whereRouteTo($route_to)->whereStartDate($date);
...
}
Use MySql date function:
$trips = Trip::where(DB::raw('DATE(route_from)'), '=', $route_from)
->where(DB::raw('DATE(route_to)'), '=', $route_to)
->where(DB::raw('DATE(start_date)'), '=', $date)
User where filter as the following:
WHERE "datetime column" like '2015-04-06%'

Categories