From 41cdeae572c3d394d913c0654d990357e139a0ff Mon Sep 17 00:00:00 2001 From: denisdulici Date: Sun, 9 Feb 2020 12:44:36 +0300 Subject: [PATCH] fixed prefix scope --- app/Http/Controllers/Settings/Company.php | 2 +- app/Http/Controllers/Settings/Defaults.php | 2 +- app/Http/Controllers/Settings/Email.php | 2 +- app/Http/Controllers/Settings/Invoice.php | 2 +- .../Controllers/Settings/Localisation.php | 2 +- app/Http/Controllers/Settings/Modules.php | 2 +- app/Http/Controllers/Settings/Schedule.php | 2 +- app/Listeners/Update/V20/Version200.php | 2 ++ app/Models/Setting/Setting.php | 29 +++++++++---------- app/Scopes/Company.php | 14 +++++---- 10 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Settings/Company.php b/app/Http/Controllers/Settings/Company.php index b5e66dfbb..7bc35145d 100644 --- a/app/Http/Controllers/Settings/Company.php +++ b/app/Http/Controllers/Settings/Company.php @@ -9,7 +9,7 @@ class Company extends Controller { public function edit() { - $setting = Setting::all('company')->map(function ($s) { + $setting = Setting::prefix('company')->get()->transform(function ($s) { $s->key = str_replace('company.', '', $s->key); return $s; diff --git a/app/Http/Controllers/Settings/Defaults.php b/app/Http/Controllers/Settings/Defaults.php index 9389fc5af..476c8a155 100644 --- a/app/Http/Controllers/Settings/Defaults.php +++ b/app/Http/Controllers/Settings/Defaults.php @@ -13,7 +13,7 @@ class Defaults extends Controller { public function edit() { - $setting = Setting::all('default')->map(function ($s) { + $setting = Setting::prefix('default')->get()->transform(function ($s) { $s->key = str_replace('default.', '', $s->key); return $s; diff --git a/app/Http/Controllers/Settings/Email.php b/app/Http/Controllers/Settings/Email.php index 5be0ed67f..2ccc438d9 100644 --- a/app/Http/Controllers/Settings/Email.php +++ b/app/Http/Controllers/Settings/Email.php @@ -31,7 +31,7 @@ class Email extends Controller public function edit() { - $setting = Setting::all('email')->map(function ($s) { + $setting = Setting::prefix('email')->get()->transform(function ($s) { $s->key = str_replace('email.', '', $s->key); return $s; diff --git a/app/Http/Controllers/Settings/Invoice.php b/app/Http/Controllers/Settings/Invoice.php index 017bab346..4b58c69e0 100644 --- a/app/Http/Controllers/Settings/Invoice.php +++ b/app/Http/Controllers/Settings/Invoice.php @@ -9,7 +9,7 @@ class Invoice extends Controller { public function edit() { - $setting = Setting::all('invoice')->map(function ($s) { + $setting = Setting::prefix('invoice')->get()->transform(function ($s) { $s->key = str_replace('invoice.', '', $s->key); return $s; diff --git a/app/Http/Controllers/Settings/Localisation.php b/app/Http/Controllers/Settings/Localisation.php index e3bf65ea6..ea95628ba 100644 --- a/app/Http/Controllers/Settings/Localisation.php +++ b/app/Http/Controllers/Settings/Localisation.php @@ -12,7 +12,7 @@ class Localisation extends Controller public function edit() { - $setting = Setting::all('localisation')->map(function ($s) { + $setting = Setting::prefix('localisation')->get()->transform(function ($s) { $s->key = str_replace('localisation.', '', $s->key); return $s; diff --git a/app/Http/Controllers/Settings/Modules.php b/app/Http/Controllers/Settings/Modules.php index 03121837a..b70c751a1 100644 --- a/app/Http/Controllers/Settings/Modules.php +++ b/app/Http/Controllers/Settings/Modules.php @@ -29,7 +29,7 @@ class Modules extends Controller */ public function edit($alias) { - $setting = Setting::all($alias)->map(function ($s) use ($alias) { + $setting = Setting::prefix($alias)->get()->transform(function ($s) use ($alias) { $s->key = str_replace($alias . '.', '', $s->key); return $s; })->pluck('value', 'key'); diff --git a/app/Http/Controllers/Settings/Schedule.php b/app/Http/Controllers/Settings/Schedule.php index 7a1ddd22c..5905c8fe4 100644 --- a/app/Http/Controllers/Settings/Schedule.php +++ b/app/Http/Controllers/Settings/Schedule.php @@ -9,7 +9,7 @@ class Schedule extends Controller { public function edit() { - $setting = Setting::all('schedule')->map(function ($s) { + $setting = Setting::prefix('schedule')->get()->transform(function ($s) { $s->key = str_replace('schedule.', '', $s->key); return $s; diff --git a/app/Listeners/Update/V20/Version200.php b/app/Listeners/Update/V20/Version200.php index 32bdad194..510bf69a9 100644 --- a/app/Listeners/Update/V20/Version200.php +++ b/app/Listeners/Update/V20/Version200.php @@ -939,8 +939,10 @@ class Version200 extends Listener 'app/Jobs/Income', 'app/Listeners/Incomes', 'app/Listeners/Updates', + 'app/Models/Company', 'app/Models/Expense', 'app/Models/Income', + 'app/Models/Item', 'app/Notifications/Expense', 'app/Notifications/Income', 'app/Overrides', diff --git a/app/Models/Setting/Setting.php b/app/Models/Setting/Setting.php index 36df75525..f18f934a0 100644 --- a/app/Models/Setting/Setting.php +++ b/app/Models/Setting/Setting.php @@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Model as Eloquent; class Setting extends Eloquent { - protected $table = 'settings'; public $timestamps = false; @@ -19,11 +18,6 @@ class Setting extends Eloquent */ protected $fillable = ['company_id', 'key', 'value']; - /** - * The "booting" method of the model. - * - * @return void - */ protected static function boot() { parent::boot(); @@ -31,21 +25,24 @@ class Setting extends Eloquent static::addGlobalScope(new Company); } - public static function all($code = 'general') - { - return static::where('key', 'like', $code . '.%')->get(); - } - - /** - * Global company relation. - * - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function company() { return $this->belongsTo('App\Models\Common\Company'); } + /** + * Scope to only include by prefix. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param $company_id + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function scopePrefix($query, $prefix = 'company') + { + return $query->where('key', 'like', $prefix . '.%'); + } + /** * Scope to only include company data. * diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index 12416909b..4017b3143 100644 --- a/app/Scopes/Company.php +++ b/app/Scopes/Company.php @@ -2,10 +2,9 @@ namespace App\Scopes; -use App; -use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Scope; class Company implements Scope { @@ -21,7 +20,12 @@ class Company implements Scope $table = $model->getTable(); // Skip for specific tables - $skip_tables = ['companies', 'jobs', 'migrations', 'notifications', 'permissions', 'role_user', 'roles', 'sessions', 'users']; + $skip_tables = [ + 'companies', 'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', + 'permissions', 'roles', 'role_companies', 'role_permissions', 'sessions', 'users', 'user_companies', + 'user_dashboards', 'user_permissions', 'user_roles', + ]; + if (in_array($table, $skip_tables)) { return; } @@ -66,4 +70,4 @@ class Company implements Scope return false; } -} \ No newline at end of file +}