From 6d1cbef7404e49ea9721d4f33afe08aad9ff82f3 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 15 May 2022 16:22:47 +1000 Subject: [PATCH] Updated Domains controller Cleaned up, Moved DB calls to model --- app/Http/Controllers/DomainsController.php | 32 +++++----------------- app/Models/Domains.php | 26 ++++++++++++++++++ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/DomainsController.php b/app/Http/Controllers/DomainsController.php index 976d54f..aed9a39 100644 --- a/app/Http/Controllers/DomainsController.php +++ b/app/Http/Controllers/DomainsController.php @@ -16,35 +16,22 @@ class DomainsController extends Controller public function index() { - $domains = DB::table('domains as d') - ->join('providers as p', 'd.provider_id', '=', 'p.id') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->get(['d.*', 'p.name as provider_name', 'pr.*']); + $domains = Domains::domainsDataIndexPage(); return view('domains.index', compact(['domains'])); } public function show(Domains $domain) { - - $service_extras = DB::table('domains as d') - ->join('providers as p', 'd.provider_id', '=', 'p.id') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->where('d.id', '=', $domain->id) - ->get(['d.*', 'p.name as provider_name', 'pr.*']); - - $labels = DB::table('labels_assigned as l') - ->join('labels', 'l.label_id', '=', 'labels.id') - ->where('l.service_id', '=', $domain->id) - ->get(['labels.label']); + $service_extras = Domains::domainsDataShowPage($domain->id); + $labels = Labels::labelsForService($domain->id); return view('domains.show', compact(['domain', 'service_extras', 'labels'])); } public function create() { - $Providers = Providers::allProviders(); - return view('domains.create', compact('Providers')); + return view('domains.create'); } public function store(Request $request) @@ -88,15 +75,9 @@ class DomainsController extends Controller public function edit(Domains $domain) { - $domain_info = DB::table('domains as d') - ->join('pricings as pr', 'd.id', '=', 'pr.service_id') - ->where('d.id', '=', $domain->id) - ->get(['d.*', 'pr.*']); + $domain_info = Domains::domainsDataEditPage($domain->id); - $labels = DB::table('labels_assigned as l') - ->join('labels', 'l.label_id', '=', 'labels.id') - ->where('l.service_id', '=', $domain->id) - ->get(['labels.id', 'labels.label']); + $labels = Labels::labelsForService($domain->id); return view('domains.edit', compact(['domain', 'domain_info', 'labels'])); } @@ -131,6 +112,7 @@ class DomainsController extends Controller Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $domain->id); + Cache::forget("labels_for_service.{$domain->id}"); Cache::forget('services_count');//Main page services_count cache Cache::forget('due_soon');//Main page due_soon cache Cache::forget('recently_added');//Main page recently_added cache diff --git a/app/Models/Domains.php b/app/Models/Domains.php index c2974d0..4e17810 100644 --- a/app/Models/Domains.php +++ b/app/Models/Domains.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\DB; class Domains extends Model { @@ -12,4 +13,29 @@ class Domains extends Model public $incrementing = false; protected $fillable = ['id', 'domain', 'extension', 'ns1', 'ns2', 'ns3', 'price', 'currency', 'payment_term', 'owned_since', 'provider_id', 'next_due_date']; + + public static function domainsDataIndexPage() + { + return DB::table('domains as d') + ->join('providers as p', 'd.provider_id', '=', 'p.id') + ->join('pricings as pr', 'd.id', '=', 'pr.service_id') + ->get(['d.*', 'p.name as provider_name', 'pr.*']); + } + + public static function domainsDataShowPage(string $domain_id) + { + return DB::table('domains as d') + ->join('providers as p', 'd.provider_id', '=', 'p.id') + ->join('pricings as pr', 'd.id', '=', 'pr.service_id') + ->where('d.id', '=', $domain_id) + ->get(['d.*', 'p.name as provider_name', 'pr.*']); + } + + public static function domainsDataEditPage(string $domain_id) + { + return DB::table('domains as d') + ->join('pricings as pr', 'd.id', '=', 'pr.service_id') + ->where('d.id', '=', $domain_id) + ->get(['d.*', 'pr.*']); + } }