Hey guys!
I was cleaning my codes today and was wondering about 2 issues I had. First issue is:
First issue:
I got News posts, which are written by users. Those news posts are all part of a project. So I got id user_id and project_id in my table. I used relations to add it directly to my project but still have to manually add the user id, is there any different way of handling this?
$request['user_id'] = Auth::user()->id;
$project->news()->save(new News($request->all()));
Second issue:
A news posts date can be changed from my backend. What we do have there is:
<input type="text" name="date" class="form-control datepicker" value="{{ $today->format('d.m.Y') }}" required>
<input type="text" name="time" class="form-control timepicker" value="{{ $today->format('H:i:s') }}" required>
I am mixing those together to a full date when receiving the request:
public function store(ManageNewsRequest $request, Project $project)
{
// Add user id and generate the correct date
$request['user_id'] = Auth::user()->id;
$request['created_at'] = [$request->input('date'),$request->input('time')];
$project->news()->save(new News($request->all()));
Session::flash('alert', 'News post added');
return redirect()->route('admin.projectadmin.news.index', $project->id);
}
And then I do change the value to a carbon object in the mutator:
public function setCreatedAtAttribute($dateAndTime)
{
$this->attributes['created_at'] = Carbon::createFromFormat('d.m.Y H:i', $dateAndTime[0].' '.$dateAndTime[1]);
}
However, any time I try to access $dateAndTime it was already converted to a carbon object. It is also an object which represents the current time, not the time I tried to set in the backend. I have checked if the request goes through and yes, I do receive all data correctly, but it is not sent to the mutator. I do have the date listed in $dates just to make sure there is no mistake.