1.5 thiết kế method add in Class Settings (ok)
Last updated
Last updated
Source code
C:\xampp82\htdocs\lva7\app\Models\Setting.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
class Setting extends BaseModel
{
use HasFactory, SoftDeletes;
protected $table = 'settings';
protected $fillable = [
'name',
'val',
'type',
];
public static function get($key, $default = null)
{
if (self::has($key)) {
$setting = self::getAllSettings()->whereStrict('name', $key)->first();
return self::castValue($setting->val, $setting->type);
};
return self::getDefaultValue($key, $default);
}
/**
* Check if setting exists.
*
* @return bool
*/
public static function has($key)
{
return (bool) self::getAllSettings()->whereStrict('name', $key)->count();
}
public static function getAllSettings()
{
return self::all();
}
/**
* caste value into respective type.
*
* @return bool|int
*/
private static function castValue($val, $castTo)
{
switch ($castTo) {
case 'int':
case 'integer':
return intval($val);
break;
case 'bool':
case 'boolean':
return boolval($val);
break;
default:
return $val;
}
}
/**
* Get default value from config if no value passed.
*
* @return mixed
*/
private static function getDefaultValue($key, $default)
{
return is_null($default) ? self::getDefaultValueForField($key) : $default;
}
private static function getDefaultValueForField($key)
{
return self::getDefinedSettingFields()
->pluck('value', 'name')
->get($key);
}
/**
* Get all the settings fields from config.
*
* @return Collection
*/
private static function getDefinedSettingFields()
{
return collect(config('setting_fields'))->pluck('elements')->flatten(1);
}
/**
* Get the validation rules for setting fields.
*
* @return array
*/
public static function getValidationRules()
{
return self::getDefinedSettingFields()->pluck('rules', 'name')
->reject(function ($val) {
return empty($val);
})->toArray();
}
public static function add($key, $val, $type = 'string')
{
if (self::has($key)) {
return self::set($key, $val, $type);
}
return self::create(['name' => $key, 'val' => $val, 'type' => $type]);
}
public static function set($key, $val, $type)
{
if ($setting = self::getAllSettings()->whereStrict('name', $key)->first()) {
return $setting->update([
'name' => $key,
'val' => $val,
'type' => $type
]) ? $val : false;
}
return self::add($key, $val, $key);
}
/**
* Get the data type of a setting.
*
* @return mixed
*/
public static function getDataType($key)
{
$type = self::getDefinedSettingFields()
->pluck('type', 'name')
->get($key);
return is_null($type) ? 'string' : $type;
}
}
C:\xampp82\htdocs\lva7\app\Models\BaseModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
use HasFactory;
public $timestamps = false;
/**
* Set 'Name' attribute value.
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = trim($value);
}
}
C:\xampp82\htdocs\lva7\app\Http\Controllers\SettingController.php
<?php
namespace App\Http\Controllers;
use App\Models\Setting;
use Illuminate\Http\Request;
class SettingController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view("backend.settings.index");
}
/**
* Show the form for creating a new resource.
*/
public function store(Request $request)
{
$rules = Setting::getValidationRules();
$data = $this->validate($request, $rules);
$validSettings = array_keys($rules);
foreach ($data as $key => $val) {
if (in_array($key, $validSettings)) {
Setting::add($key, $val,Setting::getDataType($key));
}
}
return redirect()->back()->with('status', 'Settings has been saved.');
}
}