Laravel 10 load config from database

Load cấu hình từ trong database là 1 phần không thể thiếu khi xây dựng website giúp tuỳ chỉnh website mà người dùng tương tác mà không phải nhờ đến code cấu hình trực tiếp trong file .Ở bài viết này chúng ta sẽ từng bước 1 để có thể code trong các dự án và project của mình 

1 : Tạo project laravel 10 mới 

Cài đặt một ứng dụng laravel mới, vào terminal, gõ lệnh và tạo một ứng dụng laravel mới

composer create-project laravel/laravel la10config

2 : Cấu hình database

Vào file config .env set up database như sau : 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=la10config
DB_USERNAME=root
DB_PASSWORD=

3 Tạo model, migration, seeder

Chúng ta mở terminal gõ lệnh sau :

php artisan make:model Config --all

Sau đó chúng ta vào file migration :  database/migrations/2024_01_13_083315_create_configs_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('configs', function (Blueprint $table) {
            $table->id();
            $table->string('name',300);
            $table->string('value',1000);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('configs');
    }
};

Sau đó chúng ta chạy lệnh :

php artisan migrate

File database/seeders/ConfigSeeder.php :

<?php

namespace Database\Seeders;

use App\Models\Config;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class ConfigSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $data[] = ['name' => 'website','value' =>'Demo dev.truyenvideo.com'];
        $data[] = ['name' => 'description','value' =>'Miêu tả website dev.truyenvideo.com'];
        foreach ($data as $key => $value)
        {
            Config::create($value);
        }
    }
}

File database/seeders/DatabaseSeeder.php : 

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call(ConfigSeeder::class);
    }
}

File app/Providers/AppServiceProvider.php : 

<?php

namespace App\Providers;

use App\Models\Config;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $data = Cache::remember('province.config',Carbon::now()->addDays(10),function (){
           return Config::all()->pluck('value','name')->toArray();
        });
        foreach($data as $key => $value)
        {
            \config()->set('website.'.$key,$value);
        }
        dd(\config('website.website'));
    }
}

4 Chạy lệnh server

Ở bước cuối cùng này chúng ta chạy lệnh sau : 

php artisan serve

Để chạy chương trình . Nếu đã xong thì xoá dòng :

dd(\config('website.website'));

Ở file Provider

Như vậy là chúng ta đã cấu hình demo cơ bản tạo các cấu hình được load từ database vào config .Nếu thấy hay thì bạn hãy chia sẻ cho mình nhé