Completed
Push — master ( 06d7c0...788317 )
by Arjay
01:19
created

BelongsTo::placeholder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Yajra\DataTables\Html\Editor\Fields;
4
5
use Illuminate\Support\Str;
6
7
class BelongsTo extends Select
8
{
9
    /**
10
     * @param string $class
11
     * @param string $text
12
     * @param string $id
13
     * @param string|null $foreign
14
     * @return \Yajra\DataTables\Html\Editor\Fields\Field|static
15
     */
16
    public static function model($class, $text, $id = 'id', $foreign = null)
17
    {
18
        $table   = Str::singular(app($class)->getTable());
19
        $foreign = $foreign ?? $table . '_id';
20
21
        return self::make($foreign, Str::title($table))
22
                   ->modelOptions($class, $text, $id);
23
    }
24
25
    /**
26
     * Add a placeholder and allow clear.
27
     * Note: This requires editor select2 plugin.
28
     *
29
     * @see https://editor.datatables.net/plug-ins/field-type/editor.select2
30
     * @param string $text
31
     * @param null|string $id
32
     * @param bool $allowClear
33
     * @return $this
34
     */
35
    public function placeholder($text, $id = null, $allowClear = true)
36
    {
37
        $this->type('select2')
38
             ->opts([
39
                 'allowClear'  => $allowClear,
40
                 'placeholder' => [
41
                     'id'   => $id,
42
                     'text' => $text,
43
                 ],
44
             ]);
45
46
        return $this;
47
    }
48
}
49