I am using a Laravel eloquent model.
I want to add a query condition where start_time later than now.
For example:
Model::whereAfterNow('start_time')->all()
Can you help me??
it looks like you need a query scope (https://laravel.com/docs/5.6/eloquent#local-scopes).
assuming 'start_time' is a model property (database field) containing a some representation of time, and you want a scope that returns all models where 'start_time' later than now...
How you structure your code depends on what format your dates are stored in your database.
For example, if you are using epoch timestamps, then in your Model.php:
public function scopewhereAfterNow($query)
{
return $query->where('start_time', '>', \Carbon\Carbon::now()->timestamp);
}
or you could use the DB facade:
public function scopewhereAfterNow($query)
{
return $query->where('start_time', '>', DB::raw('unix_timestamp(NOW())'));
}
which you might call like:
$results = Model::whereAfterNow()->get();
If 'start_time' returns a DateTime string you can try:
Model::where('start_time', '>', \Carbon\Carbon::now()->toDateTimeString())->get();
If 'start_time' only returns time then you can try:
Model::where('start_time', '>', \Carbon\Carbon::now()->toTimeString())->get();
Related
In my laravel project I didn't migrate the table with timestamp(created_at). I used to get the time with a date column.
I need to filter the data from that table so that I used Laravel Carbon class.
This is my function for filter the data
public function getWeeklyData()
{
$data = \Carbon\Carbon::today()->subDays(7);
$weeklydata=DB::table('response')->where('date','>=',$data)
->get();
return view('admin.pages.tables.weeklydata',['weeklydata' => $weeklydata]);
}
But there is no output.
Some times my date format is different from Carbon class date format.
If any solution for this?
When you use Carbon it will carbon object return. so try below
$data = \Carbon\Carbon::today()->subDays(7)->format('Y-m-h');
$weeklydata=DB::table('response')->where('date','>=',$data)
->get();
and you can use whereDate also
$data = \Carbon\Carbon::today()->subDays(7);
$weeklydata=DB::table('response')->whereDate('date','>=',$data)
->get();
I have a collection with records that include the created_at row. Now i want to get specific rows by date without time. I mean :
$users->whereLoose('created_at', '2016-11-23')
of course this returns empty but the format I need to use is "Y-m-d" without the "H:i:s" part. How can use this format to get records from the collection?
You could also use the filter method and the isSameDay() method.
$date = \Carbon\Carbon::parse('2016-11-23');
$usersForDate = $users->filter(function ($user) use ($date) {
return $user->created_at->isSameDay($date);
});
Hope this helps!
You can do this:
$date = Carbon::parse('2016-11-23');
$users->where('created_at', '>', $date->startOdDay())
->where('created_at', '<', $date->endOfDay());
Or you could try this:
$users->whereDate('created_at', '=', '2016-11-23');
You can try using the map() function on the collection per the docs https://laravel.com/docs/5.2/collections:
https://laravel.com/docs/5.2/collections#method-map
$collection = collect(['taylor', 'abigail', null])->map(function ($name) {
return strtoupper($name);
})
->reject(function ($name) {
return empty($name);
});
What is the function of latest() in laravel?
Example:
public function activity()
{
return $this->hasMany('App\Activity')
->with(['user', 'subject'])
->latest();
}
From Build an activity feed in Laravel on line 44.
I've been looking in the laravel documentation, but I couldn't find it...
latest() is a function defined in Illuminate\Database\Query\Builder Class. It's job is very simple. This is how it is defined.
public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}
So, It will just orderBy with the column you provide in descending order with the default column will be created_at.
->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Add ->get() on your code like this:
public function activity()
{
return $this->hasMany('App\Activity')
->with(['user', 'subject'])
->latest()->get();
}
latest() function in Laravel used to get latest records from database using default column created_at.
latest()is the equivalent to orderBy('created_at', 'desc')
When trying to find all records where the created_at time is larger than a certain value, it does not seem to work.
For example I have tried this:
return Foo::where('created_at', '>', '1457408361')->get();
But it keeps returning all the records!
Use the whereDate() query method:
$date = Carbon::yesterday(); // Create date however you want
Foo::whereDate('created_at', '>', $date);
If you do these types of queries a lot, I’d consider wrapping it into a query scope:
use DateTimeInterface;
class Foo extends Model
{
public function scopeCreatedAfter(DateTimeInterface $date)
{
return $query->whereDate('created_at', '>', $date);
}
}
Usage:
Foo::createdAfter($date)->get();
You can always use Carbon to convert the UNIX timestamp:
Foo::where('created_at', '>=', Carbon::createFromTimestamp(1457408361))->get();
So you can convert your integer to Carbon instance and compare it as above.
You can read about Carbon here: http://carbon.nesbot.com/docs/
I've got a model called "Lesson" which has a ->hasMany() relationship which represent the dates for this lesson.
To retrieve every lesson with a date within a certain time-span I use a whereHas:
$list = Lesson::whereHas('dates', function ($query) use ($start, $end) {
$query->where('date','>=', $start->format('Y-m-d'))
->where('date','<=', $end->format('Y-m-d'));
})->get()
This works as expected, but I now want to sort the $list by the 'smallest' date associated with the lesson.
Preferably I wish to use the Eloquent models, as there are some needed methods in the model. But if it's not possible, than a 'plain' sql statement could also be used.
You can use the available methods of Laravel collections.This code should do it.
$sortedLessons = $lessons->sortBy(function ($lesson) {
$dates = $lesson->dates;
$minDate = $dates->sortBy('date');
return $minDate;
});
I assumed that you have a field date in your date model.