git commit "add dynamic settings management"
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\SettingStoreRequest;
|
||||
use App\Http\Requests\Admin\SettingUpdateRequest;
|
||||
use App\Models\Setting;
|
||||
|
||||
class SettingController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$settings = Setting::query()
|
||||
->applySearch()
|
||||
->paginate(10);
|
||||
|
||||
return view('admin.settings.index', compact('settings'));
|
||||
}
|
||||
|
||||
|
||||
public function create()
|
||||
{
|
||||
$settings = Setting::all();
|
||||
return view('admin.settings.create',compact('settings'));
|
||||
|
||||
}
|
||||
|
||||
public function store(SettingStoreRequest $request)
|
||||
{
|
||||
Setting::create($request->validated());
|
||||
|
||||
return redirect()->route('admin.settings.index');
|
||||
}
|
||||
|
||||
public function edit(Setting $setting)
|
||||
{
|
||||
return view('admin.settings.edit', compact('setting'));
|
||||
|
||||
}
|
||||
|
||||
public function update(SettingUpdateRequest $request,Setting $setting)
|
||||
{
|
||||
|
||||
$setting->update($request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('admin.settings.index')
|
||||
->with('success', 'تنظیمات با موفقیت ویرایش شد.');
|
||||
|
||||
}
|
||||
|
||||
public function destroy(Setting $setting)
|
||||
{
|
||||
$setting->delete();
|
||||
|
||||
return redirect()
|
||||
->route('admin.settings.index')
|
||||
->with('success', 'محصول با موفقیت حذف شد.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SettingStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code'=>[
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'value'=>[
|
||||
'nullable',
|
||||
'string',
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SettingUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code'=>[
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'value'=>[
|
||||
'nullable',
|
||||
'string',
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+19
-1
@@ -7,11 +7,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Setting
|
||||
*
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $code
|
||||
* @property string $value
|
||||
@@ -29,4 +31,20 @@ class Setting extends Model
|
||||
'code',
|
||||
'value'
|
||||
];
|
||||
|
||||
#[Scope]
|
||||
protected function applySearch(Builder $query): void
|
||||
{
|
||||
$request = request();
|
||||
|
||||
if ($request->filled('keyword')) {
|
||||
$keyword = $request->input('keyword');
|
||||
|
||||
|
||||
$query->whereAny([
|
||||
'code',
|
||||
], 'LIKE', "%$keyword%");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
if (!function_exists('getUserFullName')) {
|
||||
function getUserFullName(?\App\Models\User $user = null): string
|
||||
{
|
||||
@@ -22,4 +24,22 @@ if (!function_exists('getImageUrl')) {
|
||||
}
|
||||
|
||||
}
|
||||
if (!function_exists('settings')) {
|
||||
function settings(?string $code): ?string
|
||||
{
|
||||
if (!$code) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$setting = Setting::query()
|
||||
->where('code', $code)
|
||||
->first();
|
||||
|
||||
if (!$setting) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $setting->value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user