mardi 23 octobre 2018

Extending DB facade Laravel

I would like to convert a timestamp and have some other values related to it. My question is how I can introduce my own method like DB::raw() that appends everything to the current select values. So, for instance, for something like this

$user = DB::table('users')
->select('*', DB::timestamp('timestamp_column', 'convert_timezone', 'called_as'))
->where('id', 1)->first();

Let's assume that I am trying to get the value for created_at column and it's called as converted_created_at and it should return something like below.

{
id: 1,
name:'John Doe',
converted_created_at: {
 'utc_time': 'created_at value as that is in utc by default',
 'converted_time': 'timestamp converted into user timezone',
 'diff': '10 hours ago' // difference between created_at and current timestamp 
}
}

So, how do I introduce my own method that does this? You can take example of any SQL database as you wish. I know I can do that with Model but I wanted to see how to approach this problem using a facade. Thank you in advance for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire