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

mr-grayda's avatar

Modifying dates returned by query

I'm teaching myself Laravel by porting an API I wrote in vanilla PHP. The API does nothing too fancy -- a scheduled task takes an assortment of text files, grabs the values and dates within and stores them in the database. Later, when requested, the data is just grabbed, almost verbatim from the database, and returned as JSON.

One feature of my original API was the ability to modify the UTC dates returned by passing a timezone as a query string (for example: http://api.example.com/v1/something?tz=Australia%2FMelbourne).

Right now I've replicated this feature by taking the results of the query in my controller, modifying them, then returning them. But I feel like there's a more Laravel-y way to do this. I've heard of people using PHP's Traits feature to do this, but I'm not sure how I'd use that.

Here's a sample controller that shows what I'm doing:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Validator;
use App\Something; // Model
use Carbon\Carbon;

class SomethingController extends Controller
{

  function get(Request $request) {
    $results = Something::select('created_at', 'value')->get();

    foreach($results as $key => $value) {
      $results[$key]['created_at'] = (new Carbon($results[$key]['created_at']))->setTimezone($request->query('tz'))->__toString();
    }

    return $results;
  }

Any suggestions?

0 likes
5 replies
andonovn's avatar
andonovn
Best Answer
Level 22

What you are doing is perfectly fine because it simply get the job done. I personally would do the same if that's something I need to achieve fast and if it's a small application where all it's doing is inside this controller.

That being said, here are few things you can consider if you still want to improve this:

  • I guess $results, $key and $value have these names for the sake of the example. But if I am wrong and that's really how you named the variables, then it would be cooler to name these better. In Laravel we value such small things.
  • Many folks would use the Collection instance you have (your $results variable) and will get rid of the foreach in favour of collection method called transform. That would also get rid of the temporary variable $results as it would be redundant. More about the method in the docs
  • If that's a bigger application, a good thing would be to clean your controller from that logic. The more "Laravel-ish" way of doing this is to create a custom request instance and move the transformation logic there. Then your controller could end up looking similar to just return $request->applyTransformers(Something::select('created_at', 'value')->get());

Edit: Oh, I don't know how I missed that Something is actually an Eloquent model. Then all you should do is to create an accessor called getCreatedAtAttribute() inside the Something class and move the transformation logic there. Then your controller could end up as simple as return Something::all(); (in case created_at and value are all the columns you have in the database, or if you don't mind selecting few columns you won't need) More about accessors in the docs

1 like
mr-grayda's avatar

@andonovn: Ah, transform looks immensely useful. I'll definitely be making use of that. I'll also be wandering through my controllers and cleaning up variable names, because they were half placeholders, half 'just want to get this to run'

These are great resources for me to read up on. Thanks!

@Cronix: That's a great idea. I always seem to forget that MySQL has a myriad of built-ins for transforming dates. With a selectRaw and passing my timezone in as a named bound parameter, I was able to get this to work well.

Thanks everyone!

Cronix's avatar

@mr-grayda You're welcome. Please mark it as solved to make it easier for others to find.

andonovn's avatar

@Cronix I agree that your solution is indeed the best (if there is such thing) approach to the problem. A "Laravel-ish" implementation of that would be to create a local Eloquent scope called something like scopeWithDatesInTimezone() so you can use it from the controller (and everywhere else) to make it look more nicely and human readable. Also, you can define that as a global scope if you want to apply it to all your queries. More about Eloquent scopes in the docs

@mr-gayda Just a ping so you can mark Cronix's reply as best one instead of mine. As that would help people in the future coming from Google to find the best approach faster.

Please or to participate in this conversation.