您可以从 Laravel 提供的配置函数中访问应用名称。
$appName = config('app.name');
// This value is retrieved from .env file of APP_NAME=
如果你必须存储多个与星期相关的值,你可以创建一个新的配置文件 week.php
//config/week.php
return [
...
'first_day_of_the_week' => 0
];
为了检索 first_day_of_the_week,您可以使用相同的函数 config
$firstDayOfTheWeek = config('week.first_day_of_the_week')
与其他基本标志类似,您可以创建一个新的配置文件。
您可以稍后使用以下命令缓存您的配置变量。
php artisan config:cache
你也可以在 laravel 项目的任何首选位置创建一个 Helper 类。我将助手类保存在 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;
}
....
}
如果需要在Laravel项目中获取方法值,可以通过
$getTodayDateFormat = App\Helpers\DateHelpers::getTodayFormat();
编辑 1:
根据问题描述。您需要在设置表中创建一行。
//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();
});
}
您只需要一行设置表即可检索/更新默认值。
//检索第一天
$first_day_of_the_week = App\Setting::first()->first_day_of_the_week;
//更新第一天
...
$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
]);