Repository navigation
Expand file tree
/
Copy pathReorderableColumnOrder.php
More file actions
84 lines (72 loc) · 2.23 KB
/
Copy pathReorderableColumnOrder.php
File metadata and controls
84 lines (72 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace NyonCode\WireSortable\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Stores user-specific column ordering per model type and table.
*
* @property int $id
* @property int $user_id
* @property string $model_type
* @property string $table_identifier
* @property array $column_order
*/
class ReorderableColumnOrder extends Model
{
protected $table = 'reorderable_column_orders';
protected $fillable = [
'user_id',
'model_type',
'table_identifier',
'column_order',
];
protected $casts = [
'column_order' => 'array',
];
/**
* @return BelongsTo<Model, self>
*/
public function user(): BelongsTo
{
$userModel = config('wire-sortable.user_model', 'App\\Models\\User');
return $this->belongsTo($userModel);
}
/**
* Get column order for a user + model + table combination.
*
* @return array<int, string>|null
*/
public static function getOrder(int|string $userId, string $modelType, string $tableIdentifier): ?array
{
$record = static::query()
->where('user_id', $userId)
->where('model_type', $modelType)
->where('table_identifier', $tableIdentifier)
->first();
return $record?->column_order;
}
/**
* Save column order for a user + model + table combination.
*
* @param array<int, string> $columnOrder
*/
public static function saveOrder(int|string $userId, string $modelType, string $tableIdentifier, array $columnOrder): void
{
static::query()->updateOrCreate(
['user_id' => $userId, 'model_type' => $modelType, 'table_identifier' => $tableIdentifier],
['column_order' => $columnOrder],
);
}
/**
* Delete column order for a user + model + table combination.
*/
public static function deleteOrder(int|string $userId, string $modelType, string $tableIdentifier): void
{
static::query()
->where('user_id', $userId)
->where('model_type', $modelType)
->where('table_identifier', $tableIdentifier)
->delete();
}
}