Fixed locations cache

Fixed locations cache
This commit is contained in:
cp6 2022-05-09 15:00:33 +10:00
parent 6da3af1364
commit d3125170e6
4 changed files with 15 additions and 7 deletions

View File

@ -31,7 +31,7 @@ class LocationsController extends Controller
'name' => $request->location_name 'name' => $request->location_name
]); ]);
Cache::forget('all_locations'); Cache::forget('locations');
return redirect()->route('locations.index') return redirect()->route('locations.index')
->with('success', 'Location Created Successfully.'); ->with('success', 'Location Created Successfully.');
@ -65,7 +65,7 @@ class LocationsController extends Controller
$items->delete(); $items->delete();
Cache::forget('all_locations'); Cache::forget('locations');
return redirect()->route('locations.index') return redirect()->route('locations.index')
->with('success', 'Location was deleted Successfully.'); ->with('success', 'Location was deleted Successfully.');

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 Locations extends Model class Locations extends Model
{ {
@ -12,4 +14,11 @@ class Locations extends Model
protected $fillable = ['name']; protected $fillable = ['name'];
protected $table = 'locations'; protected $table = 'locations';
public static function allLocations(): array
{
return Cache::remember("locations", now()->addMonth(1), function () {
return DB::table('locations')->get()->toArray();
});
}
} }

View File

@ -15,9 +15,7 @@ class LocationsSelect extends Component
*/ */
public function render() public function render()
{ {
$all_locations = Cache::rememberForever('all_locations', function () { $all_locations = Locations::allLocations();
return Locations::all();
});
return view('components.locations-select', [ return view('components.locations-select', [
'locations' => $all_locations 'locations' => $all_locations
]); ]);

View File

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