From 356bd368a42083a45c9f5b35e8487161309a2119 Mon Sep 17 00:00:00 2001 From: cp6 Date: Wed, 20 Jul 2022 14:35:12 +1000 Subject: [PATCH] Updated Misc class to use relationships + cache Updated Misc class to use relationships + cache --- app/Http/Controllers/MiscController.php | 37 +++++++++---------------- app/Models/Misc.php | 22 +++++++++++++++ resources/views/misc/edit.blade.php | 20 ++++++------- resources/views/misc/index.blade.php | 14 +++++----- resources/views/misc/show.blade.php | 32 ++++++++++----------- 5 files changed, 68 insertions(+), 57 deletions(-) diff --git a/app/Http/Controllers/MiscController.php b/app/Http/Controllers/MiscController.php index c5fc361..271e9d7 100644 --- a/app/Http/Controllers/MiscController.php +++ b/app/Http/Controllers/MiscController.php @@ -14,10 +14,7 @@ class MiscController extends Controller { public function index() { - $misc = DB::table('misc_services as d') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->get(['d.*', 'pr.*']); - + $misc = Misc::allMisc(); return view('misc.index', compact(['misc'])); } @@ -28,12 +25,8 @@ class MiscController extends Controller public function show(Misc $misc) { - $service_extras = DB::table('misc_services as m') - ->join('pricings as p', 'm.id', '=', 'p.service_id') - ->where('m.id', '=', $misc->id) - ->get(['m.*', 'p.*']); - - return view('misc.show', compact(['misc', 'service_extras'])); + $misc_data = Misc::misc($misc->id)[0]; + return view('misc.show', compact(['misc_data'])); } public function store(Request $request) @@ -45,20 +38,19 @@ class MiscController extends Controller 'next_due_date' => 'required|date' ]); - $ms_id = Str::random(8); + $misc_id = Str::random(8); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - - $pricing->insertPricing(5, $ms_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); + $pricing->insertPricing(5, $misc_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Misc::create([ - 'id' => $ms_id, + 'id' => $misc_id, 'name' => $request->name, 'owned_since' => $request->owned_since ]); + Cache::forget("all_misc"); Home::homePageCacheForget(); return redirect()->route('misc.index') @@ -67,12 +59,8 @@ class MiscController extends Controller public function edit(Misc $misc) { - $misc = DB::table('misc_services as s') - ->join('pricings as p', 's.id', '=', 'p.service_id') - ->where('s.id', '=', $misc->id) - ->get(['s.*', 'p.*']); - - return view('misc.edit', compact('misc')); + $misc_data = Misc::misc($misc->id)[0]; + return view('misc.edit', compact('misc_data')); } public function update(Request $request, Misc $misc) @@ -91,11 +79,11 @@ class MiscController extends Controller ]); $pricing = new Pricing(); - $as_usd = $pricing->convertToUSD($request->price, $request->currency); - $pricing->updatePricing($misc->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); + Cache::forget("all_misc"); + Cache::forget("misc.{$misc->id}"); Home::homePageCacheForget(); return redirect()->route('misc.index') @@ -105,12 +93,13 @@ class MiscController extends Controller public function destroy(Misc $misc) { $items = Misc::find($misc->id); - $items->delete(); $p = new Pricing(); $p->deletePricing($misc->id); + Cache::forget("all_misc"); + Cache::forget("misc.{$misc->id}"); Home::homePageCacheForget(); return redirect()->route('misc.index') diff --git a/app/Models/Misc.php b/app/Models/Misc.php index cba204b..21115b0 100644 --- a/app/Models/Misc.php +++ b/app/Models/Misc.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Cache; class Misc extends Model { @@ -14,4 +15,25 @@ class Misc extends Model protected $table = 'misc_services'; protected $fillable = ['id', 'name', 'owned_since']; + + public static function allMisc() + {//All misc and relationships (no using joins) + return Cache::remember("all_misc", now()->addMonth(1), function () { + return Misc::with(['price'])->get(); + }); + } + + public static function misc(string $misc_id) + {//Single misc and relationships (no using joins) + return Cache::remember("misc.$misc_id", now()->addMonth(1), function () use ($misc_id) { + return Misc::where('id', $misc_id) + ->with(['price'])->get(); + }); + } + + public function price() + { + return $this->hasOne(Pricing::class, 'service_id', 'id'); + } + } diff --git a/resources/views/misc/edit.blade.php b/resources/views/misc/edit.blade.php index b62c2c5..c05db6f 100644 --- a/resources/views/misc/edit.blade.php +++ b/resources/views/misc/edit.blade.php @@ -1,7 +1,7 @@ -@section('title') {{$misc[0]->name}} {{'edit'}} @endsection +@section('title') {{$misc_data->name}} {{'edit'}} @endsection - Edit {{ $misc[0]->name }} + Edit {{ $misc_data->name }}
@@ -11,7 +11,7 @@ Go back -
+ @csrf @method('PUT')
@@ -20,7 +20,7 @@
Name
+ name="name" required maxlength="255" value="{{$misc_data->name}}"> @error('name') {{ $message }} @enderror
@@ -30,17 +30,17 @@ Price price 0.01 - {{ $misc[0]->price }} + {{ $misc_data->price->price }}
- {{$misc[0]->term}} + {{$misc_data->price->term}}
- {{$misc[0]->currency}} + {{$misc_data->price->currency}}
@@ -49,20 +49,20 @@ Owned since owned_since - {{$misc[0]->owned_since }} + {{$misc_data->owned_since }}
Next due date next_due_date - {{$misc[0]->next_due_date}} + {{$misc_data->price->next_due_date}}
active === 1) ? 'checked' : '' }}> + value="1" {{ ($misc_data->active === 1) ? 'checked' : '' }}> diff --git a/resources/views/misc/index.blade.php b/resources/views/misc/index.blade.php index 74ac459..cd574b3 100644 --- a/resources/views/misc/index.blade.php +++ b/resources/views/misc/index.blade.php @@ -36,23 +36,23 @@ {{ date_format(new DateTime($m->owned_since), 'jS F Y') }} @endif - {{ now()->diffInDays($m->next_due_date) }} + {{ now()->diffInDays($m->price->next_due_date) }} days - {{$m->price}} {{$m->currency}} - {{\App\Process::paymentTermIntToString($m->term)}} + {{$m->price->price}} {{$m->price->currency}} + {{\App\Process::paymentTermIntToString($m->price->term)}} - - + - @csrf @method('DELETE') + id="btn-{{$m->name}}" title="{{$m->id}}"> diff --git a/resources/views/misc/show.blade.php b/resources/views/misc/show.blade.php index 9ba0200..8bb5e4c 100644 --- a/resources/views/misc/show.blade.php +++ b/resources/views/misc/show.blade.php @@ -1,4 +1,4 @@ -@section('title') {{ $service_extras[0]->name }} {{'service'}} @endsection +@section('title') {{ $misc_data->name }} {{'service'}} @endsection {{ __('Misc details') }} @@ -7,11 +7,11 @@
-

{{ $service_extras[0]->name}}

+

{{ $misc_data->name}}

-
{{ $service_extras[0]->service_id }}
- @if($service_extras[0]->active !== 1) +
{{ $misc_data->id }}
+ @if($misc_data->active !== 1)
not active
@endif
@@ -23,41 +23,41 @@ Service - {{ $service_extras[0]->name }} + {{ $misc_data->name }} Price - {{ $service_extras[0]->price }} {{ $service_extras[0]->currency }} - {{\App\Process::paymentTermIntToString($service_extras[0]->term)}} + {{ $misc_data->price->price }} {{ $misc_data->price->currency }} + {{\App\Process::paymentTermIntToString($misc_data->price->term)}} Owned since - @if(!is_null($service_extras[0]->owned_since)) - {{ date_format(new DateTime($service_extras[0]->owned_since), 'jS F Y') }} + @if(!is_null($misc_data->owned_since)) + {{ date_format(new DateTime($misc_data->owned_since), 'jS F Y') }} @endif Next due date - {{Carbon\Carbon::parse($service_extras[0]->next_due_date)->diffForHumans()}} - ({{Carbon\Carbon::parse($service_extras[0]->next_due_date)->format('d/m/Y')}}) + {{Carbon\Carbon::parse($misc_data->price->next_due_date)->diffForHumans()}} + ({{Carbon\Carbon::parse($misc_data->price->next_due_date)->format('d/m/Y')}}) Inserted - @if(!is_null($service_extras[0]->created_at)) - {{ date_format(new DateTime($service_extras[0]->created_at), 'jS M y g:i a') }} + @if(!is_null($misc_data->created_at)) + {{ date_format(new DateTime($misc_data->created_at), 'jS M y g:i a') }} @endif Updated - @if(!is_null($service_extras[0]->updated_at)) - {{ date_format(new DateTime($service_extras[0]->updated_at), 'jS M y g:i a') }} + @if(!is_null($misc_data->updated_at)) + {{ date_format(new DateTime($misc_data->updated_at), 'jS M y g:i a') }} @endif @@ -70,7 +70,7 @@ class="btn btn-success btn-sm mx-2"> Go back - Edit