Laravel Migration json類(lèi)型的應(yīng)用場(chǎng)景

一、保存復(fù)雜的配置

場(chǎng)景:
例如對(duì)于一個(gè)用戶系統(tǒng),需要很多字段來(lái)保存不同的配置,如果要為每個(gè)配置都添加一個(gè)字段的話,數(shù)據(jù)表會(huì)非常大,而且如果需要經(jīng)常往里面加入新的內(nèi)容,則需要經(jīng)常變更這個(gè)表的結(jié)構(gòu)

解決方案:
可以考慮用一個(gè)json字段來(lái)保存所有的配置
結(jié)合laravel model里面的cast等特性,也可以比較方便的讀取和存儲(chǔ)配置值
再結(jié)合request validation和自定義哪些字段允許設(shè)置就可以實(shí)現(xiàn)比較靈活的配置

優(yōu)劣:
優(yōu)勢(shì)是可以很靈活的新增配置,劣勢(shì)是配置的讀取和存儲(chǔ)稍復(fù)雜了一些,而且如果后期還需要對(duì)這些配置進(jìn)行查詢的話,比較困難(不過(guò)一般應(yīng)該不會(huì)有很多這方面的需求)

例子:
1、Setting類(lèi)


<?php

namespace App;

use Exception;

class Settings
{
    /**
     * The User instance.
     *
     * @var User
     */
    protected $user;

    /**
     * The list of settings.
     *
     * @var array
     */
    protected $settings = [];

    /**
     * Create a new settings instance.
     *
     * @param array $settings
     * @param User  $user
     */
    public function __construct(array $settings, User $user)
    {
        $this->settings = $settings;
        $this->user = $user;
    }

    /**
     * Retrieve the given setting.
     *
     * @param  string $key
     * @return string
     */
    public function get($key)
    {
        return array_get($this->settings, $key);
    }

    /**
     * Create and persist a new setting.
     *
     * @param string $key
     * @param mixed  $value
     */
    public function set($key, $value)
    {
        $this->settings[$key] = $value;

        $this->persist();
    }

    /**
     * Determine if the given setting exists.
     *
     * @param  string $key
     * @return boolean
     */
    public function has($key)
    {
        return array_key_exists($key, $this->settings);
    }

    /**
     * Retrieve an array of all settings.
     *
     * @return array
     */
    public function all()
    {
        return $this->settings;
    }

    /**
     * Merge the given attributes with the current settings.
     * But do not assign any new settings.
     *
     * @param  array  $attributes
     * @return mixed
     */
    public function merge(array $attributes)
    {
        $this->settings = array_merge(
            $this->settings,
            array_only($attributes, array_keys($this->settings))
        );

        return $this->persist();
    }

    /**
     * Persist the settings.
     *
     * @return mixed
     */
    protected function persist()
    {
        return $this->user->update(['settings' => $this->settings]);
    }

    /**
     * Magic property access for settings.
     *
     * @param  string $key
     * @throws Exception
     * @return
     */
    public function __get($key)
    {
        if ($this->has($key)) {
            return $this->get($key);
        }

        throw new Exception("The {$key} setting does not exist.");
    }
}

2、singleton(AppServiceProvider)

    public function register()
    {
        $this->app->singleton('App\Settings', function() {
            return User::first()->settings();
        });
    }

3、helper

<?php
function settings($key = null)
{
    $settings = app('App\Settings');
    return $key ? $settings->get($key) : $settings;
}

參考網(wǎng)站:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容