ali_254 Posted June 6, 2022 Share Posted June 6, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/ Share on other sites More sharing options...
maxxd Posted June 6, 2022 Share Posted June 6, 2022 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. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597031 Share on other sites More sharing options...
ali_254 Posted June 6, 2022 Author Share Posted June 6, 2022 (edited) If I want to use accessor or mutator, I have to write it on all models ... I have 60 models in my project! I want to change the original Eloquent class, is it possible? Thanks Edited June 6, 2022 by ali_254 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597032 Share on other sites More sharing options...
maxxd Posted June 6, 2022 Share Posted June 6, 2022 What change are you trying to make on the created_at field? 1 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597061 Share on other sites More sharing options...
ali_254 Posted June 8, 2022 Author Share Posted June 8, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597105 Share on other sites More sharing options...
maxxd Posted June 8, 2022 Share Posted June 8, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597109 Share on other sites More sharing options...
ali_254 Posted June 8, 2022 Author Share Posted June 8, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597122 Share on other sites More sharing options...
maxxd Posted June 9, 2022 Share Posted June 9, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314897-how-to-override-methods-in-elequent/#findComment-1597157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.