1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html\Editor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
|
9
|
|
|
class Options extends Collection |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Return a Yes/No options. |
13
|
|
|
* |
14
|
|
|
* @return Options |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public static function yesNo() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$data = [ |
19
|
|
|
['label' => __('Yes'), 'value' => true], |
20
|
|
|
['label' => __('No'), 'value' => false], |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
return new static($data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get options from a model. |
28
|
|
|
* |
29
|
|
|
* @param mixed $model |
30
|
|
|
* @param string $value |
31
|
|
|
* @param string $key |
32
|
|
|
* @return Collection |
33
|
|
|
*/ |
34
|
|
|
public static function model($model, $value, $key = 'id') |
35
|
|
|
{ |
36
|
|
|
if (! $model instanceof Builder) { |
37
|
|
|
$model = $model::query(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $model->get()->map(function ($model) use ($value, $key) { |
41
|
|
|
return [ |
42
|
|
|
'value' => $model->{$key}, |
43
|
|
|
'label' => $model->{$value}, |
44
|
|
|
]; |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get options from a table. |
50
|
|
|
* |
51
|
|
|
* @param mixed $table |
52
|
|
|
* @param string $value |
53
|
|
|
* @param string $key |
54
|
|
|
* @param \Closure $whereCallback |
55
|
|
|
* @param string|null $key |
56
|
|
|
* @return Collection |
57
|
|
|
*/ |
58
|
|
|
public static function table($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null) |
59
|
|
|
{ |
60
|
|
|
$query = DB::connection($connection) |
61
|
|
|
->table($table) |
62
|
|
|
->select("{$value} as label", "{$key} as value"); |
63
|
|
|
|
64
|
|
|
if ($whereCallback) { |
65
|
|
|
$query->where($whereCallback); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $query->get(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return a True/False options. |
73
|
|
|
* |
74
|
|
|
* @return Options |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public static function trueFalse() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$data = [ |
79
|
|
|
['label' => __('True'), 'value' => true], |
80
|
|
|
['label' => __('False'), 'value' => false], |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
return new static($data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Push an item onto the end of the collection. |
88
|
|
|
* |
89
|
|
|
* @param mixed $value |
90
|
|
|
* @param mixed $key |
91
|
|
|
* @return Options |
92
|
|
|
*/ |
93
|
|
|
public function append($value, $key) |
94
|
|
|
{ |
95
|
|
|
return $this->push(['label' => $value, 'value' => $key]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Push an item onto the beginning of the collection. |
100
|
|
|
* |
101
|
|
|
* @param mixed $value |
102
|
|
|
* @param mixed $key |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function prepend($value, $key = null) |
106
|
|
|
{ |
107
|
|
|
$data = ['label' => $value, 'value' => $key]; |
108
|
|
|
|
109
|
|
|
return parent::prepend($data); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.