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

BelongsTo   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A model() 0 8 1
A placeholder() 0 13 1
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