Trying to get day of week from variable queried by maxid - laravel

I'm trying to get day of week from a variable that is found by maxid, but I only get this error:
Trying to get property 'dayOfWeek' of non-object
Code:
$dailyLog = DailyLog::with(['todoList','user'])->find(\DB::table('daily_logs')->max('id'));
$weekday = $dailyLog->date->dayOfWeek;
$yesterdaysLog = DailyLog::loadByDate(Carbon::now()->subMonth(1)->next($weekday));
I am trying to get the day of week from the first query and compare it to last months same day of week

This: $dailyLog->date is not a Carbon object most probably, hence the reason why you cannot call dayOfWeek.. make sure that the date returns a Carbon instance.
To do that in your DailyLog model add this :
protected $dates = ['date'];
This will make sure that the date is cast to Carbon. But also make sure that for some reason that field is not null also.

Related

Laravel: Check if time + 4 hours has passed

I store in my database a date format like this:
2017-02-22 16:55:40
I added it to my database like this:
Carbon::now();
I need to check if 4 hours passed since this date.
How I can do this? I couldn't figure out how I can convert this format into Carbon or timestamp.
If you are using Laravel and the date is a Carbon instance from a Model you have access to the whole Carbon API.
You can use the Difference API of Carbon for this specific purpose.
echo $model->thedate->diffInHours($now, false);
If your model does not threat the date as a carbon instance you can cast it by adding the date to the dates array of the current model like so
protected $dates = [
'field_name',
];
Check out Date casting for more information
Update with an explicit example
$user = User::first();
// This will return the difference in hours
$user->created_at->diffInHours(Carbon\Carbon::now(), false);
You can convert it to a Carbon object with:
Carbon::parse('2017-02-22 16:55:40');

Laravel query for showing records starting soonest but not records where date has passed

I'm a little new so I only know the basics. I have a course table where it has the start_date of a course (timestamp) and I would like to show the courses starting closes to the current date but not courses that have passed (e.g yesterdays courses).
I also need to not do this with a scope.
My model is App\Course. My current code is
$course = Course::latest()->take(6)->get());
Could anyone help? Thanks!
Try this:
$course = Course::where('start_date', '>', Carbon::now())->orderBy('start_date', 'desc')->take(6)->get());
It will work if you'll add start_date to a $dates variable.

how to select one month back records from data base in laravel

how select one month back records from current date from database in laravel. I am trying this code.
This is controller code.
class LoginHistoryController extends Controller {
public function index()
{
$login_history = LoginHistory::where('login_date','BETWEEN', '(CURDATE() -
INTERVAL 10 DAY) AND CURDATE()' )->get();
}
}
but i am getting error.
I will have a approach something like this. First I will calculate the date like
$today = date('Y-m-d');
$date = date_create($today);
date_sub($date, date_interval_create_from_date_string("30 days"));
$beforeOneMonth = date_format($date, "Y-m-d");
You should have the intended value in $beforeOneMonth by now. Now you can compare it in anyway you like whether you use IN operator or >=. For eg.
$login_history = LoginHistory::where('login_date','>=', $beforeOneMonth)->get();
Give it a try. If you are storing date in some other format, you can do your own tricks to format the date and do the thing
Another way to do it would be with whereRaw:
$login_history = LoginHistory::whereRaw(
'login_date BETWEEN (CURDATE() - INTERVAL 10 DAY) AND CURDATE()'
)->get();
Note that whereRaw has the side effect of making your code less portable since you're using SQL that might be specific to your database server. But sometimes you just can't do what you would like using the query builder.

Laravel - convert input date format before save

I'm trying to convert date input to the proper mysql format.
My start_date field is formatted MM dd, yyyy.
In my controller store function I'm grabbing all the users input including start_date and saving.
$userProfileData->save();
What I thought I could do is us Carbon to convert the start_date before save like this.
$userProfileData->start_date->toDateString();
I get a Call to a member function toDateString() on a non-object error.
I was able to convert the date format by adding this to my users controller store function.
$userProfileData->start_date = date('Y-m-d', strtotime($userProfileData->start_date));
you get non-object error because start_date is not the object with method toDateString()
what you can do is use setStartDateAttribute() method in your model which will be invoked every time to get the start_date attribute value and in this method you can modify value so model will use the updated value for operation like edit or update it can be as follow
public function setStartDateAttribute($value) {
$this->attributes['start_date'] = date('Y-m-d', strtotime($value) );
}
the following method will used by your model when it needs to get the value of start_date attribute this method must be written in the model

date function error in rails 3

I am trying to add a date conditional to my controller index action:
#events = Event.where("date" => Date.today.to_s).order("date").page(params[:page]).per_page(5)
I am trying to make my view only show events that have a date value greater than or equal to today's date. For example if an event has a date value of 2013-05-13 it should not be shown because that event has already happened and only events with a date value of today's date or later should be shown.
The problem is, the index view isn't returning any events and I have created an with a date value of 2013-05-30 which means it should work. Any ideas?
Thanks in advance
This is actually going to return everything where the date equals today:
Event.where("date" => Date.today.to_s)
What you probably want instead is:
Event.where("date >= ?", Date.today)
ActiveRecord will interpolate the date into the ?. Also, you don't need to call to_s on it, ActiveRecord will the date for you.
Try this:
In your model use a scope like so:
scope :recent_event, lambda { where('date >= ?', Date.today ) }
Then in your controller write
#events = Event.recent_event.order("date").page(params[:page]).per_page(5)

Resources