Skip to content

Commit 5f1e035

Browse files
Extra File Added
1 parent fd4272d commit 5f1e035

File tree

17 files changed

+2420
-2
lines changed

17 files changed

+2420
-2
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# laravel-settings-component
2-
Laravel's missing setting component
1+
## About <a href="javascript:void();" target="_blank">Laravel Settings Components</a>
2+
3+
Laravel's missing setting component Feature
4+
5+
## Doc:
6+
7+
<hr/>
8+
9+
- The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
10+
11+
```
12+
App\Providers\SettingsProvider::class,
13+
```
14+
15+
- Add those route in yor routes/web.php
16+
17+
```
18+
19+
Route::prefix('settings')->group(function () {
20+
Route::get('/', [SettingController::class, 'index'])->name('admin.settings.index');
21+
Route::get('/{setting}/move-up', [SettingController::class, 'move_up'])->name('admin.settings.moveUp');
22+
Route::get('/{setting}/move-down', [SettingController::class, 'move_down'])->name('admin.settings.moveDown');
23+
Route::post('/', [SettingController::class, 'store'])->name('admin.settings.store');
24+
Route::put('/', [SettingController::class, 'update'])->name('admin.settings.update');
25+
Route::delete('/{setting}/delete', [SettingController::class, 'destroy'])->name('admin.settings.delete');
26+
Route::get('/{setting}/unset-value', [SettingController::class, 'unsetValue'])->name('admin.settings.unsetValue');
27+
});
28+
29+
```
30+
31+
## <a href="https://iqbalhasan.dev" target="_blank">iqbalhasan.dev</a> Sponsors
32+
33+
We would like to extend our thanks to the following sponsors for funding <a href="https://iqbalhasan.dev" target="_blank">iqbalhasan.dev</a> development. If you are interested in becoming a sponsor, please email us <a href="mailto:info@iqbalhasan.dev">info@iqbalhasan.dev</a>
34+
35+
## Security Vulnerabilities
36+
37+
If you discover a security vulnerability within Laravel, please send an e-mail to <a href="https://iqbalhasan.dev" target="_blank">IQBAL HASAN</a> via [info@iqbalhasan.dev](mailto:info@iqbalhasan.dev). All security vulnerabilities will be promptly addressed.
38+
39+
## License
40+
41+
The iqbalhasan.dev Project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

app/Facades/Setting.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Setting extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'setting';
17+
}
18+
}

app/Helpers/Setting.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php // Code within app\Helpers\Helper.php
2+
3+
4+
use App\Facades\Setting;
5+
6+
if (!function_exists('setting')) {
7+
function setting($key, $default = null)
8+
{
9+
return Setting::get($key, $default);
10+
}
11+
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Setting;
4+
5+
6+
use Illuminate\Support\Str;
7+
use Illuminate\Http\Request;
8+
use App\Models\Setting\Setting;
9+
use App\Http\Controllers\Controller;
10+
use Illuminate\Support\Facades\Session;
11+
use Illuminate\Support\Facades\Storage;
12+
use App\Facades\Setting as SettingFacades;
13+
14+
class SettingController extends Controller
15+
{
16+
/**
17+
* Display a listing of the resource.
18+
*
19+
* @return \Illuminate\Http\Response
20+
*/
21+
public function index()
22+
{
23+
// cache data
24+
$settings = SettingFacades::groups();
25+
26+
$groups = SettingFacades::onlyGroup();
27+
$active = (request()->session()->has('setting_tab')) ? request()->session()->get('setting_tab') : old('setting_tab', key($settings));
28+
29+
// $this->seo()->setTitle('Sittings Mangmet');
30+
31+
return \view('pages.setting.base', ["S_TYPES" => Setting::TYPES, 'settings' => $settings, 'groups' => $groups, 'active' => $active]);
32+
}
33+
/**
34+
* Store a newly created resource in storage.
35+
*
36+
* @param \Illuminate\Http\Request $request
37+
* @return \Illuminate\Http\Response
38+
*/
39+
public function store(Request $request)
40+
{
41+
$data = $request;
42+
if (!isset($request->group)) {
43+
$data['group'] = 'general';
44+
}
45+
$data['key'] = Str::lower(implode("_", explode(" ", (Str::lower($data->group)))) . '.' . \implode("_", explode(" ", $data->key)));
46+
$data['details'] = (array) \json_decode($request->details);
47+
$data['details'] = \json_encode($request->details);
48+
$data->validate([
49+
'key' => 'required|unique:settings|max:255',
50+
'display_name' => 'required|max:255',
51+
'type' => 'required|max:255',
52+
'group' => 'max:255',
53+
]);
54+
55+
$setting = new Setting;
56+
57+
$data['order'] = $setting->highestOrderSetting($data->group);
58+
59+
$setting->create($data->all());
60+
61+
// forget cache
62+
SettingFacades::forgetCache();
63+
64+
65+
session()->flash('Success', 'Successfully Create Setting');
66+
return redirect()->route('admin.settings.index');
67+
}
68+
69+
/**
70+
* Update the specified resource in storage.
71+
*
72+
* @param \Illuminate\Http\Request $request
73+
* @return \Illuminate\Http\Response
74+
*/
75+
public function update(Request $request)
76+
{
77+
78+
$setting = new Setting;
79+
80+
foreach ($request->data as $id => $value) {
81+
// return $value;
82+
$setting = $setting->find($id);
83+
$setting->group = $request->group[$id];
84+
85+
$key = explode(".", $setting->key);
86+
unset($key[0]);
87+
88+
$setting->key = implode("_", explode(" ", (Str::lower($setting->group)))) . '.' . \implode("_", $key);
89+
90+
if ($request->hasFile('data.' . $id)) {
91+
$value = Storage::putFile('setting', $request->file('data.' . $id));
92+
Storage::delete($setting->value);
93+
}
94+
95+
96+
$setting->value = $value;
97+
$setting->save();
98+
}
99+
// forget cache
100+
SettingFacades::forgetCache();
101+
102+
request()->flashOnly('setting_tab');
103+
104+
Session::flash('success', 'Setting Successfully Saved');
105+
return \redirect()->route('admin.settings.index');
106+
}
107+
108+
/**
109+
* Remove the specified resource from storage.
110+
*
111+
* @param \App\Models\Setting $setting
112+
* @return \Illuminate\Http\Response
113+
*/
114+
public function destroy(Setting $setting)
115+
{
116+
$setting->delete();
117+
118+
// forget cache
119+
SettingFacades::forgetCache();
120+
121+
Session::flash('success', 'Setting Successfully Deleted');
122+
return \redirect()->route('admin.settings.index');
123+
}
124+
125+
/**
126+
* Order the specified Setting from Settings table.
127+
*
128+
* @param \App\Models\Setting $setting
129+
* @return \Illuminate\Http\Response
130+
*/
131+
public function move_up(Setting $setting)
132+
{
133+
134+
$swapOrder = $setting->order;
135+
$previousSetting = Setting::where('order', '<', $swapOrder)
136+
->where('group', $setting->group)
137+
->orderBy('order', 'DESC')->first();
138+
139+
if (isset($previousSetting->order)) {
140+
$setting->order = $previousSetting->order;
141+
$setting->save();
142+
$previousSetting->order = $swapOrder;
143+
$previousSetting->save();
144+
145+
// forget cache
146+
SettingFacades::forgetCache();
147+
148+
Session::flash('success', $setting->display_name . ' Successfully moved up');
149+
} else {
150+
Session::flash('error', $setting->display_name . ' Already at top');
151+
}
152+
153+
request()->session()->flash('setting_tab', $setting->group);
154+
155+
return \redirect()->route('admin.settings.index');
156+
}
157+
158+
/**
159+
* Order the specified Setting from Settings table.
160+
*
161+
* @param \App\Models\Setting $setting
162+
* @return \Illuminate\Http\Response
163+
*/
164+
public function move_down(Setting $setting)
165+
{
166+
$swapOrder = $setting->order;
167+
168+
$previousSetting = Setting::where('order', '>', $swapOrder)
169+
->where('group', $setting->group)
170+
->orderBy('order', 'ASC')->first();
171+
172+
if (isset($previousSetting->order)) {
173+
$setting->order = $previousSetting->order;
174+
$setting->save();
175+
$previousSetting->order = $swapOrder;
176+
$previousSetting->save();
177+
178+
// forget cache
179+
SettingFacades::forgetCache();
180+
181+
Session::flash('success', $setting->display_name . ' Moved order down');
182+
} else {
183+
Session::flash('error', $setting->display_name . ' Already at bottom');
184+
}
185+
request()->session()->flash('setting_tab', $setting->group);
186+
187+
return \redirect()->route('admin.settings.index');
188+
}
189+
190+
/**
191+
* unset value the specified Setting from Settings table.
192+
*
193+
* @param \App\Models\Setting $setting
194+
* @return \Illuminate\Http\Response
195+
*/
196+
public function unsetValue(Setting $setting)
197+
{
198+
Storage::delete($setting->value);
199+
$setting->value = \null;
200+
$setting->save();
201+
202+
// forget cache
203+
SettingFacades::forgetCache();
204+
205+
return \redirect()->route('admin.settings.index');
206+
}
207+
}

app/Models/Setting/Setting.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Models\Setting;
4+
5+
use App\Facades\Setting as SettingFacades;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class Setting extends Model
10+
{
11+
protected $fillable = [
12+
'group',
13+
'key',
14+
'display_name',
15+
'value',
16+
'type',
17+
'details',
18+
'note',
19+
'order'
20+
];
21+
22+
const TYPES = [
23+
'text' => 'Text Box',
24+
'text_area' => 'Text Area',
25+
'rich_text_box' => 'Rich Textbox',
26+
'code_editor' => 'Code Editor',
27+
'checkbox' => 'Check Box',
28+
'radio_btn' => 'Radio Button',
29+
'select_dropdown' => 'Select Dropdown',
30+
'file' => 'File',
31+
'image' => 'Image',
32+
];
33+
34+
/**
35+
* Return the Highest Order Menu Item.
36+
*
37+
* @param number $parent (Optional) Parent id. Default null
38+
*
39+
* @return number Order number
40+
*/
41+
public function highestOrderSetting($settingGroup = null)
42+
{
43+
$order = 1;
44+
$item = $this->where('group', '=', $settingGroup)->orderBy('order', 'DESC')
45+
->first();
46+
if (!is_null($item)) {
47+
$order = intval($item->order) + 1;
48+
}
49+
return $order;
50+
}
51+
}

app/Providers/SettingsProvider.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class SettingsProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
app()->bind('setting', function () { //Keep in mind this "check" must be return from facades accessor
17+
return new \App\Setting\Setting;
18+
});
19+
}
20+
21+
/**
22+
* Bootstrap services.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
//
29+
}
30+
}

0 commit comments

Comments
 (0)