1

I wish to implement a few global app settings, e.g. App name, first day of the week, and other feature flags. The end goal is to have these be fetched and edited by administrators through the API.

What might be the most convenient way of doing this? I've experimented with using a Settings model to store key-value pairs, but this doesn't make sense to me since the desired settings should be hard coded and won't change, and seeding the Settings table doesn't sound ideal. Thanks in advance!

cameraguy258
  • 609
  • 2
  • 9
  • 21

1 Answers1

0

You can access App name from Laravel provided config function.

$appName = config('app.name'); 
// This value is retrieved from .env file of APP_NAME=

If you have to store multiple values related with the week, you can create a new config file week.php

//config/week.php
return [
    ...
    'first_day_of_the_week' => 0
]; 

In order to retrieve the first_day_of_the_week, you can use the same function config

$firstDayOfTheWeek = config('week.first_day_of_the_week')

Similar to other essential flags, you can create a new config file. You can later cache your config variables using the following command.

php artisan config:cache

You can also create a Helper class inside any preferred location inside the laravel project. I keep my helper class inside App\Helpers.

<?php

namespace App\Helpers;

use Carbon\Carbon;

class DateHelpers
{
    public const DATE_RANGE_SEPARATOR = ' to ';

    public static function getTodayFormat($format = 'Y-m-d')
    {
        $today = Carbon::now();
        $todayDate = Carbon::parse($today->format($format));
        return $todayDate;
    }
    ....
}

If you need to retrieve the method value in the Laravel project, you can access by

$getTodayDateFormat = App\Helpers\DateHelpers::getTodayFormat();

EDIT 1:

Based on the question description. You need to create one row in the settings table.

//create_settings_table.php Migration File
public function up()
    {
        // Create table for storing roles
        Schema::create('settings', function (Blueprint $table) {
            $table->increments('id');
            $table->string('app_name')->default("My App Name");
            $table->unsignedInteger('first_day_of_the_week')->default(1);
            ....
            $table->timestamps();
        });
    }

You only need one row of the settings table to retrieve/update the default value.

//Retrieving the first day

$first_day_of_the_week = App\Setting::first()->first_day_of_the_week;

//Updating the first day

...
$first_day_of_the_week = request('first_day_of_the_week');
App\Setting::first()->update([
    'first_day_of_the_week' => $first_day_of_the_week
]);
Lizesh Shakya
  • 2,482
  • 2
  • 18
  • 42