# 1. 4 Thiết kế database ánh xạ với front-end && function setting 😎(ok)

Source code

{% file src="/files/AIFcJn01fvu2oL3BUrd2" %}

<figure><img src="/files/TNmcULusEugWBjhQL5rg" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\lva7\config\setting\_fields.php

```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
<?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 👇🏽**

<figure><img src="/files/9qqFmTHK57TyRUenadCq" alt=""><figcaption></figcaption></figure>

Khó nhất là đoạn xử lý logic này

C:\xampp82\htdocs\lva7\config\setting\_fields.php

<figure><img src="/files/2g3wA3hlBdOGGm7UshN2" alt=""><figcaption></figcaption></figure>

Chuyển về dạng Collection để xử lý

```blade
{{dd(collect(config('setting_fields')));}}
```

<figure><img src="/files/jVMrYDhKj4QnjiQLhcIG" alt=""><figcaption></figcaption></figure>

Nhặt phần tử elements

```blade
{{dd(collect(config('setting_fields'))->pluck('elements'));}}
```

<figure><img src="/files/G8lfmjPOrlEBfzJxi8UH" alt=""><figcaption></figcaption></figure>

Gộp các phần tử&#x20;

```blade
{{dd(collect(config('setting_fields'))->pluck('elements')->flatten(1));}}
```

<figure><img src="/files/vocPWje56OKVACs5MU9Y" alt=""><figcaption></figcaption></figure>

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
<?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
      ],
    ]
  ],
];

```

<figure><img src="/files/sAmAXbEr619aHYSRzuxZ" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/laravel-advanced/1.-4-thiet-ke-database-anh-xa-voi-front-end-and-and-function-setting-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
