From 11690ae50e6f28f0b03360d9d8575c523e3ca2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 5 Nov 2019 00:22:36 +0100 Subject: [PATCH] (v2): Automatic resolving on route binding (#3035) * Resolve route binding * add withTrashed() property * Remove route binds from the Provider --- app/Models/BaseModel.php | 14 +++- app/Providers/RouteServiceProvider.php | 91 -------------------------- 2 files changed, 12 insertions(+), 93 deletions(-) diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index ee0a769d2411..7ee4447210c8 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -150,6 +150,16 @@ class BaseModel extends Model return new ClientSettings($this->settings); } - - + /** + * Retrieve the model for a bound value. + * + * @param mixed $value + * @return \Illuminate\Database\Eloquent\Model|null + */ + public function resolveRouteBinding($value) + { + return $this + ->withTrashed() + ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); + } } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index c001879d24d2..324b870a2598 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -40,98 +40,7 @@ class RouteServiceProvider extends ServiceProvider public function boot() { // - - parent::boot(); - - Route::bind('client', function ($value) { - $client = \App\Models\Client::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - return $client; - }); - - Route::bind('user', function ($value) { - $user = \App\Models\User::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - return $user; - }); - - Route::bind('invoice', function ($value) { - return \App\Models\Invoice::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('recurring_invoice', function ($value) { - return \App\Models\RecurringInvoice::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('recurring_quote', function ($value) { - return \App\Models\RecurringQuote::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('quote', function ($value) { - return \App\Models\Quote::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('payment', function ($value) { - return \App\Models\Payment::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('product', function ($value) { - return \App\Models\Product::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('company', function ($value) { - return \App\Models\Company::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('company_gateway', function ($value) { - return \App\Models\CompanyGateway::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('companies', function ($value) { - return \App\Models\Company::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('account', function ($value) { - return \App\Models\Account::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('client_contact', function ($value) { - return \App\Models\ClientContact::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('expense', function ($value) { - return \App\Models\Expense::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('invoice_invitation', function ($value) { - return \App\Models\InvoiceInvitation::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('recurring_invoice_invitation', function ($value) { - return \App\Models\RecurringInvoiceInvitation::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('quote_invitation', function ($value) { - return \App\Models\QuoteInvitation::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('task', function ($value) { - return \App\Models\Task::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('tax_rate', function ($value) { - return \App\Models\TaxRate::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('proposal', function ($value) { - return \App\Models\Proposal::where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - Route::bind('group_setting', function ($value) { - return \App\Models\GroupSetting::where('id', $this->decodePrimaryKey($value))->firstOrFail(); - }); - - - } /**