how to set my custom timestamp in laravel? - laravel-4

how to change timestamp to myCustomTimestamp, for example jalali timestamp
e.g : 2015-10-6 convert to 1993-7-6 and saved as created_at or updated_at in database.

I managed to find this laravel bundle, which would help you tackle jalali dates more easily - https://github.com/sallar/laravel-jdate .
I think, however, that the easier approach would be to specify your created_at and updated_at dates explicitly when doing insert/update operations, rather than extending the Eloquent class and overwriting the setUpdatedAt(), setCreatedAt() and updateTimestamps() methods (which I don't know, if it would even work and not break something).

Related

Laravel - Automatically store history in column

Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004

Eloquent - Adding custom field like timestamps in all model by default with logged user data

I would like to add to all models on my Laravel project two custom fields called updated_by and created_by, automatically filled and saved on db with the name of the logged user (I'm using JetStream), exactly like the fields updated_at and created_at.
I wonder if there is a clean procedure, before starting to add the fields on model one-by-one, and handle them in the controllers, in every store/update function I have. Extending Model and using the new class for every models seems a good solution, but I'm not really sure. And I do not know how and where to fill one-time the fields with logger user data.
Thanks, I'm newbie in Laravel and hope this is not a basic question.

When to use request in Laravel?

I am reading an article casting created_at to a string as it will return as an object by default. When to make request exactly? I know that the request can be used to transform one format to another. But when exactly? Any rules like when you create a data column you must use it? Things like that?
Using created_at as a string is not a best practice. Laravel Eloquent will automatically extract them from DB and cast them to Carbon instance, so you can transform it with a string (with ->format()) whenever you want.
Plus, having a Carbon object, you have access to lots of methods to handle your date, like comparison, extraction of single data (like day, month, hours...), so it doesn't make sense to cast it to a string, imho.

How to update updated_at field for a Sequel dataset?

I am using Sequel gem to update a dataset.
If you do an update on the model instance, the updated_at timestamp gets updated just fine:
foo = Foo[id: 1]
foo.update(blah: blah)
However, when you call a batch update on the dataset, it does not update the updated_at timestamps for every entry.
Foo.where(id: 1).update(blah: blah)
I looked at the Sequel docs regarding the timestamps plugin, which I am using in my code already, but the updated_at fields will still not be updated using the batch update. Can someone help me with this issue?
Running Foo.where(id: 1).update(blah: blah) executes a single query on the database, which is not going to run model instance-level hooks. You can either update each record individually:
Foo.where(id: 1).all{|f| f.update(blah: blah)}
or you can switch to a database-trigger based approach, such as https://github.com/jeremyevans/sequel_postgresql_triggers
There's an update_all function you could look at. It's an active record method so it should trigger the updated_at.
Update: okay i just checked and it does not trigger, you should use his answer!

Changing updated_at in a Rails record

I have a memory based session table containing the following:
id
sessnhash
userid
created_at
updated_at
The idea is that when a user first logs in a random sessnhash is created and passed back so that all further actions have to send that sessionhash for server requests. These go through a loggedin? method on the application controller which simply checks that a row in the table exists for that sessionhash.
I now want to extend this functionality to keep a tab of the last time any activity happened for that user and I thought one of way of doing this would be to immediately do a save after finding the sessionhash, hoping that this would then update the 'updated_at' attribute.
However Rails is too clever and doesn't update anything as in reality nothing has been updated.
Is there any way of forcing Rails to update the updated_at attribute without having to make any data changes to any of the other attributes?
I've just learnt that I need to do.
sessn.touch
which will update the updated_at attribute.
It's all in here: touch.

Resources