I'm having a problem with my 'end_date' column on my database. First off, I'm making a To-do application. So I have a table called 'Tasks' with following Columns:
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->mediumText('description');
$table->timestamp('end_date');
$table->integer('completed');
$table->timestamps();
When a user creates a task they can give an end_date to this task. I'm in the UTC+2 timezone so for the user I'm displaying all times in UTC+2 but i'm storing them in UTC on my database.
Here are the input fields with the actual Store function:
{{Form::date('end_date', \Carbon\Carbon::now()->timezone('Europe/Brussels'),['class' => 'form-control mb-2 mr-sm-2 mb-sm-0'])}}
{{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i'),['class' => 'form-control'])}}
Because I'm using 2 input fields I have to merge them together in the store function:
public function store(Request $request)
{
$requestData = $request->all();
$task = new Task($requestData);
$inputDateAndTime = $requestData['end_date'].' '.$requestData['time'].':00';
$task['end_date'] = Carbon::createFromFormat('Y-m-d H:i:s', $inputDateAndTime, 'Europe/Brussels')->timezone('UTC');
$task['user_id'] = auth()->id();
$task['completed'] = 0;
$task->save();
return redirect('/tasks')->with('success', 'Task created');
}
In this function I specify that the user has given a Time in UTC+2 timezone and I want this to be converted in UTC. (This works as expected)
The problem: So if the user wishes to complete this task, they can do a PUT request to update the 'completed' column, But when this request is being handled my end_date column is being changed on the same time without touching the column at all.
My update function:
public function update(Request $request, Task $task)
{
$task -> completed = 1;
$task->save();
return redirect('/tasks')->with('success', 'Task completed');
}
Example of the problem:
Let us say that user 1 creates a task with following end date = 08/18/2018 20:14
Note that this time is for UTC+2, so I'm saying convert this to UTC.
After the store function, I check the database, with following end_date = 2018-08-18 18:14:00
Which is correct, now lets see what my end_date is when I run the update function:
public function update(Request $request, Task $task)
{
dd($task->end_date);
// the result:
Carbon #1534616040 {#552 ▼ date: 2018-08-18 18:14:00.0 utc(+00:00)
}
}
So still the same value, but If I run the update function as normal and check the database this is my value:
2018-08-18 17:24:27, what I noticed is that my updated_at field has the same last 2 digits, so it is somehow taking the current time I would assume. My updated_at field: 2018-08-18 15:24:27
Any idea what I'm doing wrong here?
Timestamp is controlled by the db do that when a record is updated, it will reflect that. To have a custom field that only changes when you want it to, use datetime.
Related
I have two models in my application, Appointment and Date, an appointment has many dates and a date belongs to an appointment, now I want to search for appointments with dates which were created in a specific month, I do this by looking at appointments with dates, where this dates fullDate field is equal to the passed month, I use eloquent whereMonth for this.
Sadly the functions returns no results despite being results, I suspect this might be because Date model fullDate field is a string instead of a timestamp so maybe that's why it's failing, I tried casting fullDate to timestamp but I'm getting the same results.
fullDate is a string like this: "26-3-2020".
This is my controller method:
public function list2(Request $request)
{
$month = $request->input('month');
$appointments = Appointment::whereHas('dates', function ($query) use ($month){
$query->whereMonth('fullDate', $month);
})->with(['user'])->latest()->paginate($request->input('paginate'));
}
Date model migrations:
public function up()
{
Schema::create('dates', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('appointment_id')->index();
$table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade')->onUpdate('cascade');
$table->string('fullDate');//34-3-2020
$table->timestamps();
});
}
Any idea why it's failing.
fullDate column type should be timestamp,
and you should add this in the Date model:
protected $dates = [
'fullDate',
];
I'm creating database notifications. I have 2 Notification classes:
InterviewRequestReceived.php:
$user = Auth::user();
$interviewRequestReceived = InterviewRequestsReceived::latest()->where('user_id', $user->id)->get();
return [
'date_time1' => $interviewRequestReceived[$interviewRequestReceived->count() - 1]->latest()->get('date_time1'),
'date_time2' => $interviewRequestReceived[$interviewRequestReceived->count() - 1]->latest()->get('date_time2')
];
and InterviewRequestSent.php:
public function toDatabase($notifiable)
{
$user = Auth::user();
$interviewRequestSent = InterviewRequestsSent::latest()->where('user_id', $user->id)->get();
return [
'date_time1' => $interviewRequestSent[$interviewRequestSent->count() - 1]->latest()->get('date_time1'),
'date_time2' => $interviewRequestSent[$interviewRequestSent->count() - 1]->latest()->get('date_time2')
];
}
I have 3 tables. interview_requests_receiveds, interview_requests_sents and I created the notifications table and migrated.
On my form I have 2 options for datetime fields, so Employers can choose 2 possible date and times that would work for them to interview a candidate.
My form is working.
My notifications are working except it's inserting all of the logged in Users' date and times, instead of just the last inserted record.
I'm trying to use latest(), but it's not working.
As per Laravel Documentation
The latest and oldest methods allow you to easily order results
by date. By default, result will be ordered by the created_at
column. Or, you may pass the column name that you wish to sort by
If you want to use latest then instead of get() use first() function:
If there is already created_at column in your table then
$interviewRequestSent = InterviewRequestsSent::latest()->where('user_id', $user->id)->first();
OR
Assuming you want to get last record based on id column
$interviewRequestSent = InterviewRequestsSent::latest('id')->where('user_id', $user->id)->first();
Or you can get the last record using any below methods
InterviewRequestsSent::where('user_id', $user->id)->last();
OR
InterviewRequestsSent::where('user_id', $user->id)->orderBy('id', 'desc')->first();`
Reference:
Laravel -> Query Builder -> latest method
I'm trying to display some items from a table and I'm ordering by the time they begin, I only want to show the next three items starting from now(). Here is my controller function:
Probably the whole thing is completely wrong, but still, any help will be greatly appreciated.
public function next(Request $request)
{
$eventos = Event::orderBy('start','asc');
$eventTime = Carbon::createFromDate($eventos->start);
$mytime = Carbon::now();
if($eventTime > $mytime){
$eventos = paginate(3);
return view('evento.next',compact('eventos'));
}
}
public function next(Request $request)
{
$eventos=Event::where('start','>=',Carbon::now()) // select all recodrs where start Time more than now date
->orderBy('start','asc') // order all records by asc
->take(3) // take first 3 record
->get(); // now get the result
return view('evento.next',compact('eventos'));
}
I want to create uniqueid based on timestamps in laravel.
In laravel, Timestamps become 2 column, namely created_at and updated_at.
like this timestamps laravel
I want to combine created_at and updated_at column for unique id without dash (-) and (:).
sample output (for first row in screenshot): 2016091510453920160915104539
I used this code for $transid column:
public function insertFund($request,$lender, $type)
{
$transid = timestamp();
$fund = $this->fund->create([
'transid' => $transid
]);
return $fund;
}
But, i get error when submit.
is it possible to write code for this here?
Thanks in Advance.
i try this, and it works for me
$now = Carbon::now();
$unique_code = $now->format('YmdHisu')
the output is
20180410171800873514
if you parse you can see 2018 04 10 17:18:00 873514
I don't know what exactly you are trying to do. I understood that you want to create a record and fill the transid with concatenated timestamps.
The timestamps before the are empty as the record hasn't created yet.
In your case i would create the record and after that i would get the timestamps and update the same record.
Example code:
$fund = Fund::create($input);
$transid = $fund->created_at.$fund->updated_at;
$fund->transid = $transid;
$fund->update();
If this is not what you are trying to do , provide more information in order to help.
I am working on an application which uses Laravel and Eloquent that determines whether something (a job) has not been completed on time. This is stored as a DATE within the table. E.g. 2016-07-18
I am trying to get all the records where todays date is greater than the "completed_date" stored inside the table.
Here is what I have tried:
$id = \Auth::user()->id;
$now = \Carbon\Carbon::today();
$jobs = Job::whereHas('manager', function ($user) use ($id) {
$user->where('users.id', '=', $id);
})->where('complete_date', '>', $now)->get();
The issue is that it's still retrieving records that are in the future. For example, the record it returns, I checked the table and the "complete_date" is 2016-08-31
Any ideas please?
Your $now variable is a Carbon object, not a date.
You have to convert the object to a date:
$now = \Carbon\Carbon::now()->format('Y-m-d');
Then you have to use this variable in your model query.