0

I have implemented a table called schedules, it has a field called day.
day is tinyInteger datatype and represents a day of week like Saturday or Wednesday.
As of getting query and presenting to the view how do I convert tinyInt to a representational String?
Also I'm concerned about MVC architecture as representing a day's string is related to the view and not model itself, so what's your opinion about creating a helper class and converting integer to string there?

2 Answers 2

3

I would do it in an accessor:

http://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators

So perhaps something like this in your Schedules model:

public function getDayAttribute($day)
{
    // Assuming your integer value is between 0 and 6
    if($day >= 0 && $day <= 6) {
        return jddayofweek($day, 1);
    }

    return null;
}

Note this assumes your integer is between 0 and 6, and 0 is a Monday.

See jddayofweek for details.

So now when you call $schedule->day you will get a string like Monday.

Alternatively, if you don't want to override the day attribute entirely like this, you can add a pseudo attribute to your model:

protected $appends = ['day_name'];

public function getDayNameAttribute()
{
    // Assuming your integer value is between 0 and 6
    if($this->day >= 0 && $this->day <= 6) {
        return jddayofweek($this->day, 1);
    }

    return null;
}

Now you can use $schedule->day to get the integer value, or $schedule->day_name to get the string.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for this too, it depends on how "often" you would need this attribute. If you have 100 controllers and you only use it once, use the repository method, vice versa on the inverse.
@jszobody with this method how you provide multi linguistic support?
@Mehrdad There are a number of ways I can think of doing it, but my gut would be to NOT handle that part in the model. Have the model return a date string in default language (assuming english here), and then later on when displaying your views, make use of Laravel's Localization feature to translate the day string. That's my first thought anyway.
@jszobody what's your opinion on keeping model and day as it is, and have a helper class that one of it's methods map's day integer to a string?
@Mehrdad I could see that working well with something like the Transformer pattern. Using something like fractal.thephpleague.com for example. I do that in one of my bigger more complex apps, where I need to modify all kinds of output. On the other hand, I wouldn't go this route just for this one field. Eloquent accessors are exactly there for this reason, for simple use cases. Just depends on the complexity of your app and what other data needs transforming for output.
0

I would recommend looking into this library: https://packagist.org/packages/nesbot/carbon It has more than 7 million installs through composer.

Basically, it will provide you with a ton of translation utilities that are generally not provided by the DateTime built in class.

The Laravel way is to inject a repository into the controller wherever you need to "interpret" your model's data. https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.