1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 1/26/2019 |
6
|
|
|
* Time: 6:00 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\types; |
10
|
|
|
|
11
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption; |
12
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join; |
13
|
|
|
use crocodicstudio\crudbooster\types\select\SelectModel; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
|
16
|
|
|
class Select |
17
|
|
|
{ |
18
|
|
|
use DefaultOption, Join; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $table string |
22
|
|
|
* @param $key_field string |
23
|
|
|
* @param $display_field string |
24
|
|
|
* @param $SQLCondition string|callable |
25
|
|
|
*/ |
26
|
|
|
public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) { |
27
|
|
|
$data = DB::table($table); |
28
|
|
|
if($SQLCondition && is_callable($SQLCondition)) { |
29
|
|
|
$data = call_user_func($SQLCondition, $data); |
30
|
|
|
}elseif ($SQLCondition && is_string($SQLCondition)) { |
31
|
|
|
$data->whereRaw($SQLCondition); |
32
|
|
|
} |
33
|
|
|
$data = $data->get(); |
34
|
|
|
$options = []; |
35
|
|
|
foreach ($data as $d) { |
36
|
|
|
$options[ $d->$key_field ] = $d->$display_field; |
37
|
|
|
} |
38
|
|
|
$data = columnSingleton()->getColumn($this->index); |
39
|
|
|
/** @var $data SelectModel */ |
40
|
|
|
$data->setOptionsFromTable(["table"=>$table,"key_field"=>$key_field,"display_field"=>$display_field]); |
41
|
|
|
columnSingleton()->setColumn($this->index, $data); |
42
|
|
|
|
43
|
|
|
$this->options($options); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function options($data_options) { |
47
|
|
|
$data = columnSingleton()->getColumn($this->index); |
48
|
|
|
/** @var $data SelectModel */ |
49
|
|
|
$data->setOptions($data_options); |
50
|
|
|
|
51
|
|
|
columnSingleton()->setColumn($this->index, $data); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
} |