Jump to content

how to override methods in elequent


ali_254

Recommended Posts

hi...

I want when receiving information from the database , Make a change in one of the fields.

I do not want to write the change function in the field on all models.

I want to make changes to all the eloquent classes. (override methods).

 

In all tables, I have a history field(created_at).

I want to make the desired changes to this field when I receive the information

 

Link to comment
Share on other sites

Depending on what it is you're trying to do with the data, there are several ways to change a field. You can set up an accessor or mutator or use a query scope, for instance. Query scope sounds like what you're looking for, although should worse comes to worst you could just write a trait and use it on your model instances where needed.

  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

On 6/7/2022 at 2:41 AM, maxxd said:

What change are you trying to make on the created_at field?

The date is stored in the database in the "Gregorian date" format.
I want to turn it into "solar history" when I receive the information

Link to comment
Share on other sites

4 hours ago, maxxd said:

According to the documentation, Laravel automatically casts the created_at and updated_at columns into Carbon instances. I don't know what 'solar history' means, but it's possible the hard work is already done and all you've got to do is format the field as desired.

I mean:

Solar Hijri Calender

Link to comment
Share on other sites

OK, so unfortunately Carbon doesn't have a native formatter for that. So one way or another, I think you're going to have to touch all of your models as they all extend from Illuminate\Database\Eloquent\Model and if you override that base class you'll have to update all the use statements. That being the case, I'd look to either global scopes or accessors to handle your case.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.