😅1. 4 Thiết kế database ánh xạ với front-end && function setting 😎(ok)
Last updated
Last updated
Source code
C:\xampp82\htdocs\lva7\config\setting_fields.php
<?php
return [
'app' => [
'title' => 'General',
'icon' => 'fas fa-cube',
'desc' => 'All the general settings for application.',
'elements' => [
[
'type' => 'text', // input fields type
'name' => 'app_name', // unique name for field
'label' => 'App Name', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2|max:50', // validation rule of laravel
'value' => 'Laravel Starter' // default value if you want
],
[
'type' => 'text', // input fields type
'name' => 'footer_text', // unique name for field
'label' => 'Footer Text', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2', // validation rule of laravel
'value' => '<a href="#" class="text-muted">Built with ♥ from Bangladesh</a>' // default value if you want
],
]
],
];
C:\xampp82\htdocs\lva7\database\migrations\2023_10_26_041539_create_settings_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('settings', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('val')->nullable();
$table->char('type', 10)->default('string');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('settings');
}
};
Chú ý: Chủ yếu phần 1.4 xây dựng chức năng này 👇🏽
Khó nhất là đoạn xử lý logic này
C:\xampp82\htdocs\lva7\config\setting_fields.php
Chuyển về dạng Collection để xử lý
{{dd(collect(config('setting_fields')));}}
Nhặt phần tử elements
{{dd(collect(config('setting_fields'))->pluck('elements'));}}
Gộp các phần tử
{{dd(collect(config('setting_fields'))->pluck('elements')->flatten(1));}}
Chú ý: Nếu cho config dạng mảng như này nó sẽ gộp các phần từ lại với nhau
<?php
return [
'app' => [
'title' => 'General',
'icon' => 'fas fa-cube',
'desc' => 'All the general settings for application.',
'elements' => [
[
'type' => 'text', // input fields type
'name' => 'app_name', // unique name for field
'label' => 'App Name', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2|max:50', // validation rule of laravel
'value' => 'Laravel Starter' // default value if you want
],
[
'type' => 'text', // input fields type
'name' => 'footer_text', // unique name for field
'label' => 'Footer Text', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2', // validation rule of laravel
'value' => '<a href="#" class="text-muted">Built with ♥ from Bangladesh</a>' // default value if you want
],
]
],
'test' => [
'title' => 'General',
'icon' => 'fas fa-cube',
'desc' => 'All the general settings for application.',
'elements' => [
[
'type' => 'text', // input fields type
'name' => 'app_name', // unique name for field
'label' => 'App Name', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2|max:50', // validation rule of laravel
'value' => 'Laravel Starter' // default value if you want
],
[
'type' => 'text', // input fields type
'name' => 'footer_text', // unique name for field
'label' => 'Footer Text', // you know what label it is
'class' => '', // any class for input
'rules' => 'required|min:2', // validation rule of laravel
'value' => '<a href="#" class="text-muted">Built with ♥ from Bangladesh</a>' // default value if you want
],
]
],
];