Remove binary attributes from DB, should be json columns
This commit is contained in:
29
app/Casts/CollectionOrNull.php
Normal file
29
app/Casts/CollectionOrNull.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CollectionOrNull implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
|
||||
{
|
||||
return collect(json_decode($value, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
|
||||
{
|
||||
return $value->count() ? json_encode($value) : NULL;
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Traits\SiteID;
|
||||
|
||||
/**
|
||||
@@ -19,7 +20,7 @@ class Charge extends Model
|
||||
use SiteID;
|
||||
|
||||
protected $casts = [
|
||||
'attributes' => 'json',
|
||||
'attributes' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
protected $dates = [
|
||||
|
@@ -7,8 +7,10 @@ use Clarkeash\Doorman\Facades\Doorman;
|
||||
use Clarkeash\Doorman\Models\Invite;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Leenooks\Casts\LeenooksCarbon;
|
||||
use Leenooks\Traits\ScopeActive;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Interfaces\IDs;
|
||||
use App\Traits\PushNew;
|
||||
|
||||
@@ -37,8 +39,8 @@ class Invoice extends Model implements IDs
|
||||
|
||||
protected $casts = [
|
||||
'created_at' => 'datetime:Y-m-d',
|
||||
'due_at' => 'datetime:Y-m-d',
|
||||
'reminders' => 'json',
|
||||
'due_at' => LeenooksCarbon::class,
|
||||
'reminders' => CollectionOrNull::class,
|
||||
'_paid_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models\Service;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Models\SupplierHostServer;
|
||||
use App\Traits\ServiceDomains;
|
||||
|
||||
@@ -13,6 +14,10 @@ class Host extends Type
|
||||
{
|
||||
use ServiceDomains;
|
||||
|
||||
protected $casts = [
|
||||
'server_data' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
protected $table = 'service_host';
|
||||
protected $with = [
|
||||
'tld',
|
||||
|
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Casts\CollectionOrNull;
|
||||
use App\Traits\SiteID;
|
||||
|
||||
class UserOauth extends Model
|
||||
@@ -13,7 +14,7 @@ class UserOauth extends Model
|
||||
protected $table = 'user_oauth';
|
||||
|
||||
protected $casts = [
|
||||
'oauth_data'=>'json',
|
||||
'oauth_data' => CollectionOrNull::class,
|
||||
];
|
||||
|
||||
public function User()
|
||||
|
Reference in New Issue
Block a user