Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Synatex's avatar

Issue with setting date (createdAt) mutator and eloquent relation

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.

0 likes
7 replies
ftrillo's avatar
ftrillo
Best Answer
Level 4

The created_at and updated_at are not really intended for what you're doing. They keep track of whenever a row was inserted or last updated in database, and Eloquent automatically includes them in some queries.

It might be simpler for you to just let those fields be and create a different one. Like published_at or publish_date or whatever.

That should result in less unexpected behaviour. I'm sorry I don't see where in your code the error comes from specifically.

Althou it is a bit weird that in the view you are setting both date and time to the year, month and day. Shouldn't you use hours, minutes and seconds for time?

1 like
Synatex's avatar

Regarding the view: I just copied the first line 2 times, I am sorry. Edited it. Do you have any idea regarding issue #1? Thanks for the help already!

Snapey's avatar

I've not seen a good way to associate a model with two others than manually setting the ID.

Perhaps it would be less offensive with something like;

$project->news()->addUser()->save(new News($request->all()));

and then create an addUser function on the model that uses Auth to set the user_id

If you want to use created_at in this way you will need to set $timestamps=false in the model to stop Eloquent interfering with your value.

1 like
ftrillo's avatar

@Snapey I'm confused about the addUser() thing. If news() returns a HasMany or a BelongsToMany object. What would the method look like and where would it be for it to achieve the desired effect and chain like that?

Snapey's avatar

@ftrillo sorry, yes you are probably right. What about this;

$project->news()->save(new News($request->all())->addUser());

obviously the add user needs to work on the News model not the project (or the builder!)

1 like
ftrillo's avatar

@Snapey Yes, that should work.

I would still not consider it a good way to associate a model with two others, like you said.

Please or to participate in this conversation.