SiteSettingModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class SiteSetting extends BaseModel implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
protected $guarded=['id'];
public static $keys = [
"app_name" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "App Title"
],
"app_slogan" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "App Slogan"
],
"secondary_phone_number" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Secondary Phone Number"
],
"address" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Address"
],
"primary_email" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Primary Email"
],
"secondary_email" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Secondary Email"
],
"map_location" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Map Location"
],
"registration_number" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Registration Number"
],
"facebook_url" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Facebook URL"
],
"twitter_url" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Twitter URL"
],
"linkedin_url" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "LinkedIn URL"
],
"youtube_url" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Youtube URL"
],
"instagram_url" => [
"type" => "text",
"element" => "text",
"visible" => 1,
"display_text" => "Instagram URL"
],
"logo" => [
"type" => "image",
"element" => "image",
"visible" => 1,
"display_text" => "Site Logo"
],
];
}
SiteSetting Controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SiteSetting\CreateSiteSettingRequest;
use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SiteSettingController extends Controller
{
public function index($settingable_type = null, $settingable_id = null)
{
$site_setting = SiteSetting::where("settingable_type", $settingable_type)
->where("settingable_id", $settingable_id)
->get();
$data = [];
foreach ($site_setting as $item) {
if ($item->type == 'image') {
$data[$item->key] = $item->getFirstMediaUrl();
} else {
$data[$item->key] = $item->value;
}
}
return $data;
}
public function store(CreateSiteSettingRequest $request)
{
try{
DB::transaction(function () use ($request) {
foreach (SiteSetting::$keys as $key => $data) {
if (!isset($request->$key))
continue;
$value = (($data["type"] == "image")
? $request->file($key) : $request->get($key));
if (!$value) {
if ($data["type"] != "image")
SiteSetting::where('key', $key)->delete();
continue;
}
$site_setting = SiteSetting::updateOrCreate([
"key" => $key,
], [
"value" => $data["type"] == "text" ? $value :
(($data["type"] == "array") ? json_encode($value) : null),
"type" => $data["type"]
]);
if ($data["type"] == "image") {
$site_setting->clearMediaCollection();
$site_setting->addMedia($request->
file($key))->toMediaCollection();
}
}
});
sweetalert()->addSuccess("Site setting added successfully!");
return back();
}
catch(\Exception $e){
return back()->with('error',$e->getMessage());
}
}
}
SiteSetting Request.php
public function rules(): array
{
return
$data = [
"settingable_type" => ["nullable"],
"settingable_id" => ["nullable"]
];
foreach (SiteSetting::$keys as $key => $type) {
$data[$key] = ["nullable", $type == "image" ? "image" : ""];
}
return $data;
}
SiteSetting APi Controller
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\SiteSetting\CreateSiteSettingRequest;
use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SiteSettingController extends Controller
{
public function index($settingable_type = null, $settingable_id = null)
{
$site_setting = SiteSetting::where("settingable_type",
$settingable_type)
->where("settingable_id", $settingable_id)
->get();
$data = [];
foreach ($site_setting as $item) {
if ($item->type == 'image') {
$data[$item->key] = $item->getFirstMediaUrl();
} else {
$data[$item->key] = $item->value;
}
}
return $data;
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateSiteSettingRequest $request)
{
DB::transaction(function () use ($request) {
// SiteSettingService::deleteUselessSiteSettings(tenant("id"));
foreach (SiteSetting::$keys as $key => $data) {
if (!isset($request->$key))
continue;
$value = (($data["type"] == "image") ?
$request->file($key) : $request->get($key));
if (!$value) {
if ($data["type"] != "image")
SiteSetting::where('key', $key)->delete();
continue;
}
$site_setting = SiteSetting::updateOrCreate([
"key" => $key,
], [
"value" => $data["type"] == "text" ?
$value : (($data["type"] == "array") ? json_encode($value) : null),
"type" => $data["type"]
]);
if ($data["type"] == "image") {
$site_setting->clearMediaCollection();
$site_setting->addMedia($request->file($key))->
toMediaCollection();
}
}
});
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Site Setting Migration
<?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('site_settings', function (Blueprint $table) {
$table->id();
$table->string("settingable_type")->nullable();
$table->string("settingable_id")->nullable();
$table->string('key');
$table->longText('value')->nullable();
$table->string('type');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('site_settings');
}
};