Completed
Push — master ( 8b5b73...3f6146 )
by Jianhua
14:01
created

EntityFieldRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 30
c 3
b 1
f 0
dl 0
loc 61
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A add() 0 3 1
A find() 0 3 1
A update() 0 3 1
A getByEntityId() 0 4 1
A list() 0 24 1
A getFields() 0 4 1
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Repository\Admin;
7
8
use App\Model\Admin\EntityField;
9
use App\Repository\Searchable;
10
11
class EntityFieldRepository
12
{
13
    use Searchable;
14
15
    public static function list($perPage, $condition = [])
16
    {
17
        $data = EntityField::query()
18
            ->where(function ($query) use ($condition) {
19
                Searchable::buildQuery($query, $condition);
20
            })
21
            ->with('entity')
22
            ->orderBy('id', 'desc')
23
            ->paginate($perPage);
24
        $formTypes = config('light.form_type');
25
        $data->transform(function ($item) use ($formTypes) {
26
            xssFilter($item);
27
            $item->entityName = $item->entity->name;
28
            $item->form_type = $formTypes[$item->form_type];
29
            $item->editUrl = route('admin::entityField.edit', ['id' => $item->id]) . '?entity_id=' . $item->entity->id;
30
            $item->deleteUrl = route('admin::entityField.delete', ['id' => $item->id]);
31
            return $item;
32
        });
33
34
        return [
35
            'code' => 0,
36
            'msg' => '',
37
            'count' => $data->total(),
38
            'data' => $data->items(),
39
        ];
40
    }
41
42
    public static function add($data)
43
    {
44
        return EntityField::query()->create($data);
45
    }
46
47
    public static function update($id, $data)
48
    {
49
        return EntityField::query()->where('id', $id)->update($data);
50
    }
51
52
    public static function find($id)
53
    {
54
        return EntityField::query()->find($id);
55
    }
56
57
    public static function delete($id)
58
    {
59
        return EntityField::destroy($id);
60
    }
61
62
    public static function getByEntityId($id)
63
    {
64
        return  EntityField::query()->where('entity_id', $id)
65
            ->orderBy('order')->orderBy('is_show_inline')->get();
66
    }
67
68
    public static function getFields($entityId)
69
    {
70
        return  EntityField::query()->select('name')->where('entity_id', $entityId)->get()
71
            ->pluck('name')->toArray();
72
    }
73
}
74