Laravel Carbon class timestamp - laravel

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

Related

Laravel Eloquent Relation Two Model

I have two model. User, Images. How can I get all the users who has images and the image date is between start and end date. Date column name "date".
You can try using Eloquent whereHas().
So the code should look like:
$users= User::whereHas('images', function($query) use ($start_date, $end_date) {
return $query->where([
['date', > , $start_date],
['date', < , $end_sate],
]);
})->get();
Document: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
Do not forget to format the date to however you want. Considering using whereDate()/whereBetween() is advised. Just play around till you get it right.
in controller class write a query to fetch all user with who has images and date is between the start and end date
$from = date('2018-01-01');
$to = date('2018-05-02');
$users= DB::table('users')
->join('images', 'users.is', '=', 'images.user_id')
->select('users.*')
->whereBetween('date', [$from , $to])
->distinct('users.id')
->get();

Laravel - How to Transform Raw Query to Eloquent in Laravel

How do I transform my raw query to Laravel Eloquent
I want to transform
$games = DB::table('game_point')
->select('game_point.game_name','game_point.description','game_point.point_assigned', DB::raw("DATE(game_point.created_at) as created_at"))
->paginate(15);
to
$games= new GamePoint();
To try this,
Here you change your date format in Date() function.
$data = GamePoint::select('game_point.game_name','game_point.description','game_point.point_assigned','game_point.created_at')->paginate(15);
foreach($data as $key => $val){
$data[$key]->created_at = Date('Y-m-d',strtotime($data[$key]->created_at));
}
You can directly change it by using date() in blade or in controller depend upon your usage.
try the following code
$data = GamePoint::select('game_point.game_name','game_point.description','game_point.point_assigned','game_point.created_at')->paginate(15);
Or In the blade or contrller you can use it just paste it where you using inside foreach
date('Y-m-d', strtotime($user->from_date));
Or you can use carbon which is already included in laravel for working with date and time.
You can read about this package here
try below:
$games = GamePoint::paginate(15);

Laravel query where date more recent than current time

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

Laravel Eloquent Relations: ->latest()

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')

How to query created_at field in Laravel

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/

Resources