Fixed OS cache

Fixed OS cache
This commit is contained in:
cp6 2022-05-09 14:57:38 +10:00
parent 06a3d71222
commit 6da3af1364
6 changed files with 17 additions and 11 deletions

View File

@ -230,7 +230,7 @@ class ApiController extends Controller
protected function getAllOs() protected function getAllOs()
{ {
$os = OS::all()->toJson(JSON_PRETTY_PRINT); $os = OS::allOS()->toJson(JSON_PRETTY_PRINT);
return response($os, 200); return response($os, 200);
} }

View File

@ -10,7 +10,7 @@ class OsController extends Controller
{ {
public function index() public function index()
{ {
$os = OS::all(); $os = OS::allOS();
return view('os.index', compact(['os'])); return view('os.index', compact(['os']));
} }
@ -29,7 +29,7 @@ class OsController extends Controller
'name' => $request->os_name 'name' => $request->os_name
]); ]);
Cache::forget('all_os'); Cache::forget('operating_systems');
return redirect()->route('os.index') return redirect()->route('os.index')
->with('success', 'OS Created Successfully.'); ->with('success', 'OS Created Successfully.');
@ -41,7 +41,7 @@ class OsController extends Controller
$items->delete(); $items->delete();
Cache::forget('all_os'); Cache::forget('operating_systems');
return redirect()->route('os.index') return redirect()->route('os.index')
->with('success', 'OS was deleted Successfully.'); ->with('success', 'OS was deleted Successfully.');

View File

@ -83,7 +83,7 @@ class ServerController extends Controller
{ {
$Providers = Providers::all(); $Providers = Providers::all();
$Locations = Locations::all(); $Locations = Locations::all();
$Os = OS::all(); $Os = OS::allOS();
return view('servers.create', compact(['Providers', 'Locations', 'Os'])); return view('servers.create', compact(['Providers', 'Locations', 'Os']));
} }

View File

@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class OS extends Model class OS extends Model
{ {
@ -12,4 +14,11 @@ class OS extends Model
protected $fillable = ['name']; protected $fillable = ['name'];
protected $table = 'os'; protected $table = 'os';
public static function allOS(): array
{
return Cache::remember("operating_systems", now()->addMonth(1), function () {
return DB::table('os')->get()->toArray();
});
}
} }

View File

@ -10,11 +10,8 @@ class OsSelect extends Component
{ {
public function render() public function render()
{ {
$all_os = Cache::rememberForever('all_os', function () {
return OS::all();
});
return view('components.os-select', [ return view('components.os-select', [
'os' => $all_os 'os' => OS::allOS()
]); ]);
} }
} }

View File

@ -2,8 +2,8 @@
<div class="input-group-prepend"><span class="input-group-text">{{ $title ??'OS'}}</span></div> <div class="input-group-prepend"><span class="input-group-text">{{ $title ??'OS'}}</span></div>
<select class="form-control" name="{{$name ?? 'os_id'}}"> <select class="form-control" name="{{$name ?? 'os_id'}}">
@foreach ($os as $o) @foreach ($os as $o)
<option value="{{ $o['id'] }}" {{(isset($current) && (string)$current === (string)$o['id'])? 'selected' : ''}}> <option value="{{ $o->id }}" {{(isset($current) && (string)$current === (string)$o->id)? 'selected' : ''}}>
{{ $o['name'] }} {{ $o->name }}
</option> </option>
@endforeach @endforeach
</select> </select>