@@ -9,65 +9,65 @@ |
||
9 | 9 | |
10 | 10 | class NotificationsController extends BaseApiController |
11 | 11 | { |
12 | - /** |
|
13 | - * List of all route actions that the base api controller |
|
14 | - * will skip permissions check for them. |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $skipPermissionCheck = ['markAsRead', 'markAllAsRead', 'list', 'unread']; |
|
12 | + /** |
|
13 | + * List of all route actions that the base api controller |
|
14 | + * will skip permissions check for them. |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $skipPermissionCheck = ['markAsRead', 'markAllAsRead', 'list', 'unread']; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Init new object. |
|
21 | - * |
|
22 | - * @param NotificationRepository $repo |
|
23 | - * @param CoreConfig $config |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function __construct(NotificationRepository $repo, CoreConfig $config) |
|
27 | - { |
|
28 | - parent::__construct($repo, $config, 'App\Modules\Notifications\Http\Resources\Notification'); |
|
29 | - } |
|
19 | + /** |
|
20 | + * Init new object. |
|
21 | + * |
|
22 | + * @param NotificationRepository $repo |
|
23 | + * @param CoreConfig $config |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function __construct(NotificationRepository $repo, CoreConfig $config) |
|
27 | + { |
|
28 | + parent::__construct($repo, $config, 'App\Modules\Notifications\Http\Resources\Notification'); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Retrieve all notifications of the logged in user. |
|
33 | - * |
|
34 | - * @param integer $perPage Number of rows per page default all data. |
|
35 | - * @return \Illuminate\Http\Response |
|
36 | - */ |
|
37 | - public function list($perPage = 0) |
|
38 | - { |
|
39 | - return $this->modelResource::collection($this->repo->list($perPage)); |
|
40 | - } |
|
31 | + /** |
|
32 | + * Retrieve all notifications of the logged in user. |
|
33 | + * |
|
34 | + * @param integer $perPage Number of rows per page default all data. |
|
35 | + * @return \Illuminate\Http\Response |
|
36 | + */ |
|
37 | + public function list($perPage = 0) |
|
38 | + { |
|
39 | + return $this->modelResource::collection($this->repo->list($perPage)); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Retrieve unread notifications of the logged in user. |
|
44 | - * |
|
45 | - * @param integer $perPage Number of rows per page default all data. |
|
46 | - * @return \Illuminate\Http\Response |
|
47 | - */ |
|
48 | - public function unread($perPage = 0) |
|
49 | - { |
|
50 | - return $this->modelResource::collection($this->repo->unread($perPage)); |
|
51 | - } |
|
42 | + /** |
|
43 | + * Retrieve unread notifications of the logged in user. |
|
44 | + * |
|
45 | + * @param integer $perPage Number of rows per page default all data. |
|
46 | + * @return \Illuminate\Http\Response |
|
47 | + */ |
|
48 | + public function unread($perPage = 0) |
|
49 | + { |
|
50 | + return $this->modelResource::collection($this->repo->unread($perPage)); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Mark the notification as read. |
|
55 | - * |
|
56 | - * @param integer $id Id of the notification. |
|
57 | - * @return \Illuminate\Http\Response |
|
58 | - */ |
|
59 | - public function markAsRead($id) |
|
60 | - { |
|
61 | - return new GeneralResource($this->repo->markAsRead($id)); |
|
62 | - } |
|
53 | + /** |
|
54 | + * Mark the notification as read. |
|
55 | + * |
|
56 | + * @param integer $id Id of the notification. |
|
57 | + * @return \Illuminate\Http\Response |
|
58 | + */ |
|
59 | + public function markAsRead($id) |
|
60 | + { |
|
61 | + return new GeneralResource($this->repo->markAsRead($id)); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Mark all notifications as read. |
|
66 | - * |
|
67 | - * @return \Illuminate\Http\Response |
|
68 | - */ |
|
69 | - public function markAllAsRead() |
|
70 | - { |
|
71 | - return new GeneralResource($this->repo->markAllAsRead()); |
|
72 | - } |
|
64 | + /** |
|
65 | + * Mark all notifications as read. |
|
66 | + * |
|
67 | + * @return \Illuminate\Http\Response |
|
68 | + */ |
|
69 | + public function markAllAsRead() |
|
70 | + { |
|
71 | + return new GeneralResource($this->repo->markAllAsRead()); |
|
72 | + } |
|
73 | 73 | } |
@@ -4,28 +4,28 @@ |
||
4 | 4 | |
5 | 5 | class SettingRepository extends AbstractRepository |
6 | 6 | { |
7 | - /** |
|
8 | - * Return the model full namespace. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - protected function getModel() |
|
13 | - { |
|
14 | - return 'App\Modules\Core\Setting'; |
|
15 | - } |
|
7 | + /** |
|
8 | + * Return the model full namespace. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + protected function getModel() |
|
13 | + { |
|
14 | + return 'App\Modules\Core\Setting'; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * Save list of settings. |
|
19 | - * |
|
20 | - * @param array $data |
|
21 | - * @return void |
|
22 | - */ |
|
23 | - public function saveMany(array $data) |
|
24 | - { |
|
25 | - \DB::transaction(function () use ($data) { |
|
26 | - foreach ($data as $value) { |
|
27 | - $this->save($value); |
|
28 | - } |
|
29 | - }); |
|
30 | - } |
|
17 | + /** |
|
18 | + * Save list of settings. |
|
19 | + * |
|
20 | + * @param array $data |
|
21 | + * @return void |
|
22 | + */ |
|
23 | + public function saveMany(array $data) |
|
24 | + { |
|
25 | + \DB::transaction(function () use ($data) { |
|
26 | + foreach ($data as $value) { |
|
27 | + $this->save($value); |
|
28 | + } |
|
29 | + }); |
|
30 | + } |
|
31 | 31 | } |
@@ -6,37 +6,37 @@ |
||
6 | 6 | class Setting extends Model |
7 | 7 | { |
8 | 8 | |
9 | - use SoftDeletes; |
|
10 | - protected $table = 'settings'; |
|
11 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | - protected $hidden = ['deleted_at']; |
|
13 | - protected $guarded = ['id', 'key']; |
|
14 | - protected $fillable = ['name', 'value']; |
|
15 | - public $searchable = ['name', 'value', 'key']; |
|
9 | + use SoftDeletes; |
|
10 | + protected $table = 'settings'; |
|
11 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | + protected $hidden = ['deleted_at']; |
|
13 | + protected $guarded = ['id', 'key']; |
|
14 | + protected $fillable = ['name', 'value']; |
|
15 | + public $searchable = ['name', 'value', 'key']; |
|
16 | 16 | |
17 | - public function getCreatedAtAttribute($value) |
|
18 | - { |
|
19 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
20 | - } |
|
17 | + public function getCreatedAtAttribute($value) |
|
18 | + { |
|
19 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
20 | + } |
|
21 | 21 | |
22 | - public function getUpdatedAtAttribute($value) |
|
23 | - { |
|
24 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | - } |
|
22 | + public function getUpdatedAtAttribute($value) |
|
23 | + { |
|
24 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | + } |
|
26 | 26 | |
27 | - public function getDeletedAtAttribute($value) |
|
28 | - { |
|
29 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | - } |
|
27 | + public function getDeletedAtAttribute($value) |
|
28 | + { |
|
29 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | + } |
|
31 | 31 | |
32 | - public function newCollection(array $models = []) |
|
33 | - { |
|
34 | - return parent::newCollection($models)->keyBy('key'); |
|
35 | - } |
|
32 | + public function newCollection(array $models = []) |
|
33 | + { |
|
34 | + return parent::newCollection($models)->keyBy('key'); |
|
35 | + } |
|
36 | 36 | |
37 | - public static function boot() |
|
38 | - { |
|
39 | - parent::boot(); |
|
40 | - Setting::observe(\App::make('App\Modules\Core\ModelObservers\SettingsObserver')); |
|
41 | - } |
|
37 | + public static function boot() |
|
38 | + { |
|
39 | + parent::boot(); |
|
40 | + Setting::observe(\App::make('App\Modules\Core\ModelObservers\SettingsObserver')); |
|
41 | + } |
|
42 | 42 | } |
@@ -6,14 +6,14 @@ |
||
6 | 6 | |
7 | 7 | class General extends JsonResource |
8 | 8 | { |
9 | - /** |
|
10 | - * Transform the resource into an array. |
|
11 | - * |
|
12 | - * @param \Illuminate\Http\Request $request |
|
13 | - * @return array |
|
14 | - */ |
|
15 | - public function toArray($request) |
|
16 | - { |
|
17 | - return ['status' => $this->resource ?? 'success']; |
|
18 | - } |
|
9 | + /** |
|
10 | + * Transform the resource into an array. |
|
11 | + * |
|
12 | + * @param \Illuminate\Http\Request $request |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + public function toArray($request) |
|
16 | + { |
|
17 | + return ['status' => $this->resource ?? 'success']; |
|
18 | + } |
|
19 | 19 | } |
@@ -6,28 +6,28 @@ |
||
6 | 6 | |
7 | 7 | class Setting extends JsonResource |
8 | 8 | { |
9 | - /** |
|
10 | - * Indicates if the resource's collection keys should be preserved. |
|
11 | - * |
|
12 | - * @var bool |
|
13 | - */ |
|
14 | - public $preserveKeys = true; |
|
9 | + /** |
|
10 | + * Indicates if the resource's collection keys should be preserved. |
|
11 | + * |
|
12 | + * @var bool |
|
13 | + */ |
|
14 | + public $preserveKeys = true; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Transform the resource into an array. |
|
18 | - * |
|
19 | - * @param \Illuminate\Http\Request $request |
|
20 | - * @return array |
|
21 | - */ |
|
22 | - public function toArray($request) |
|
23 | - { |
|
24 | - return [ |
|
25 | - 'id' => $this->id, |
|
26 | - 'name' => $this->name, |
|
27 | - 'value' => $this->value, |
|
28 | - 'key' => $this->key, |
|
29 | - 'createdAt' => $this->created_at, |
|
30 | - 'updatedAt' => $this->updated_at, |
|
31 | - ]; |
|
32 | - } |
|
16 | + /** |
|
17 | + * Transform the resource into an array. |
|
18 | + * |
|
19 | + * @param \Illuminate\Http\Request $request |
|
20 | + * @return array |
|
21 | + */ |
|
22 | + public function toArray($request) |
|
23 | + { |
|
24 | + return [ |
|
25 | + 'id' => $this->id, |
|
26 | + 'name' => $this->name, |
|
27 | + 'value' => $this->value, |
|
28 | + 'key' => $this->key, |
|
29 | + 'createdAt' => $this->created_at, |
|
30 | + 'updatedAt' => $this->updated_at, |
|
31 | + ]; |
|
32 | + } |
|
33 | 33 | } |
@@ -10,36 +10,36 @@ |
||
10 | 10 | |
11 | 11 | class SettingsController extends BaseApiController |
12 | 12 | { |
13 | - /** |
|
14 | - * The validations rules used by the base api controller |
|
15 | - * to check before add. |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected $validationRules = [ |
|
19 | - 'id' => 'required|exists:settings,id', |
|
20 | - 'value' => 'required|string' |
|
21 | - ]; |
|
13 | + /** |
|
14 | + * The validations rules used by the base api controller |
|
15 | + * to check before add. |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected $validationRules = [ |
|
19 | + 'id' => 'required|exists:settings,id', |
|
20 | + 'value' => 'required|string' |
|
21 | + ]; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Init new object. |
|
25 | - * |
|
26 | - * @param SettingRepository $repo |
|
27 | - * @param CoreConfig $config |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function __construct(SettingRepository $repo, CoreConfig $config) |
|
31 | - { |
|
32 | - parent::__construct($repo, $config, 'App\Modules\Core\Http\Resources\Setting'); |
|
33 | - } |
|
23 | + /** |
|
24 | + * Init new object. |
|
25 | + * |
|
26 | + * @param SettingRepository $repo |
|
27 | + * @param CoreConfig $config |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function __construct(SettingRepository $repo, CoreConfig $config) |
|
31 | + { |
|
32 | + parent::__construct($repo, $config, 'App\Modules\Core\Http\Resources\Setting'); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Save list of settings. |
|
37 | - * |
|
38 | - * @param \Illuminate\Http\Request $request |
|
39 | - * @return \Illuminate\Http\Response |
|
40 | - */ |
|
41 | - public function saveMany(Request $request) |
|
42 | - { |
|
43 | - return new GeneralResource($this->repo->saveMany($request->all())); |
|
44 | - } |
|
35 | + /** |
|
36 | + * Save list of settings. |
|
37 | + * |
|
38 | + * @param \Illuminate\Http\Request $request |
|
39 | + * @return \Illuminate\Http\Response |
|
40 | + */ |
|
41 | + public function saveMany(Request $request) |
|
42 | + { |
|
43 | + return new GeneralResource($this->repo->saveMany($request->all())); |
|
44 | + } |
|
45 | 45 | } |
@@ -6,633 +6,633 @@ |
||
6 | 6 | |
7 | 7 | abstract class AbstractRepository implements RepositoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * The model implementation. |
|
11 | - * |
|
12 | - * @var object |
|
13 | - */ |
|
14 | - public $model; |
|
9 | + /** |
|
10 | + * The model implementation. |
|
11 | + * |
|
12 | + * @var object |
|
13 | + */ |
|
14 | + public $model; |
|
15 | 15 | |
16 | - /** |
|
17 | - * The config implementation. |
|
18 | - * |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $config; |
|
16 | + /** |
|
17 | + * The config implementation. |
|
18 | + * |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $config; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Create new AbstractRepository instance. |
|
25 | - */ |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - $this->config = \CoreConfig::getConfig(); |
|
29 | - $this->model = \App::make($this->getModel()); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Fetch all records with relations from the storage. |
|
34 | - * |
|
35 | - * @param array $relations |
|
36 | - * @param string $sortBy |
|
37 | - * @param boolean $desc |
|
38 | - * @param array $columns |
|
39 | - * @return collection |
|
40 | - */ |
|
41 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
42 | - { |
|
43 | - $sort = $desc ? 'desc' : 'asc'; |
|
44 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Fetch all records with relations from storage in pages |
|
49 | - * that matche the given query. |
|
50 | - * |
|
51 | - * @param string $query |
|
52 | - * @param integer $perPage |
|
53 | - * @param array $relations |
|
54 | - * @param string $sortBy |
|
55 | - * @param boolean $desc |
|
56 | - * @param array $columns |
|
57 | - * @return collection |
|
58 | - */ |
|
59 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | - { |
|
61 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
62 | - $conditionColumns = $this->model->searchable; |
|
63 | - $sort = $desc ? 'desc' : 'asc'; |
|
64 | - |
|
65 | - /** |
|
66 | - * Construct the select conditions for the model. |
|
67 | - */ |
|
68 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
69 | - |
|
70 | - if (count($conditionColumns)) { |
|
71 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
72 | - if (Str::contains($column, '->')) { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) { |
|
86 | - $column = 'LOWER('.$column.')'; |
|
87 | - if (Str::contains($column, '->')) { |
|
88 | - $column = $this->wrapJsonSelector($column); |
|
89 | - } |
|
90 | - |
|
91 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Loop through the model relations. |
|
96 | - */ |
|
97 | - foreach ($relations as $relation) { |
|
98 | - /** |
|
99 | - * Remove the sub relation if exists. |
|
100 | - */ |
|
101 | - $relation = explode('.', $relation)[0]; |
|
102 | - |
|
103 | - /** |
|
104 | - * Try to fetch the relation repository from the core. |
|
105 | - */ |
|
106 | - if (\Core::$relation()) { |
|
107 | - /** |
|
108 | - * Construct the relation condition. |
|
109 | - */ |
|
110 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
111 | - |
|
112 | - $subModel->where(function ($q) use ($query, $relation) { |
|
113 | - |
|
114 | - /** |
|
115 | - * Get columns of the relation. |
|
116 | - */ |
|
117 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
118 | - |
|
119 | - if (count($subConditionColumns)) { |
|
120 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
121 | - if (Str::contains($column, '->')) { |
|
122 | - $column = $this->wrapJsonSelector($column); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Use the first element in the relation model columns to construct the first condition. |
|
127 | - */ |
|
128 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Loop through the rest of the columns to construct or where conditions. |
|
133 | - */ |
|
134 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
135 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
136 | - if (Str::contains($column, '->')) { |
|
137 | - $column = $this->wrapJsonSelector($column); |
|
138 | - } |
|
23 | + /** |
|
24 | + * Create new AbstractRepository instance. |
|
25 | + */ |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + $this->config = \CoreConfig::getConfig(); |
|
29 | + $this->model = \App::make($this->getModel()); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Fetch all records with relations from the storage. |
|
34 | + * |
|
35 | + * @param array $relations |
|
36 | + * @param string $sortBy |
|
37 | + * @param boolean $desc |
|
38 | + * @param array $columns |
|
39 | + * @return collection |
|
40 | + */ |
|
41 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
42 | + { |
|
43 | + $sort = $desc ? 'desc' : 'asc'; |
|
44 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Fetch all records with relations from storage in pages |
|
49 | + * that matche the given query. |
|
50 | + * |
|
51 | + * @param string $query |
|
52 | + * @param integer $perPage |
|
53 | + * @param array $relations |
|
54 | + * @param string $sortBy |
|
55 | + * @param boolean $desc |
|
56 | + * @param array $columns |
|
57 | + * @return collection |
|
58 | + */ |
|
59 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
60 | + { |
|
61 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
62 | + $conditionColumns = $this->model->searchable; |
|
63 | + $sort = $desc ? 'desc' : 'asc'; |
|
64 | + |
|
65 | + /** |
|
66 | + * Construct the select conditions for the model. |
|
67 | + */ |
|
68 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
69 | + |
|
70 | + if (count($conditionColumns)) { |
|
71 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
72 | + if (Str::contains($column, '->')) { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) { |
|
86 | + $column = 'LOWER('.$column.')'; |
|
87 | + if (Str::contains($column, '->')) { |
|
88 | + $column = $this->wrapJsonSelector($column); |
|
89 | + } |
|
90 | + |
|
91 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Loop through the model relations. |
|
96 | + */ |
|
97 | + foreach ($relations as $relation) { |
|
98 | + /** |
|
99 | + * Remove the sub relation if exists. |
|
100 | + */ |
|
101 | + $relation = explode('.', $relation)[0]; |
|
102 | + |
|
103 | + /** |
|
104 | + * Try to fetch the relation repository from the core. |
|
105 | + */ |
|
106 | + if (\Core::$relation()) { |
|
107 | + /** |
|
108 | + * Construct the relation condition. |
|
109 | + */ |
|
110 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
111 | + |
|
112 | + $subModel->where(function ($q) use ($query, $relation) { |
|
113 | + |
|
114 | + /** |
|
115 | + * Get columns of the relation. |
|
116 | + */ |
|
117 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
118 | + |
|
119 | + if (count($subConditionColumns)) { |
|
120 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
121 | + if (Str::contains($column, '->')) { |
|
122 | + $column = $this->wrapJsonSelector($column); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Use the first element in the relation model columns to construct the first condition. |
|
127 | + */ |
|
128 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Loop through the rest of the columns to construct or where conditions. |
|
133 | + */ |
|
134 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
135 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
136 | + if (Str::contains($column, '->')) { |
|
137 | + $column = $this->wrapJsonSelector($column); |
|
138 | + } |
|
139 | 139 | |
140 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
141 | - } |
|
142 | - }); |
|
143 | - }); |
|
144 | - } |
|
145 | - } |
|
146 | - }); |
|
140 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
141 | + } |
|
142 | + }); |
|
143 | + }); |
|
144 | + } |
|
145 | + } |
|
146 | + }); |
|
147 | 147 | |
148 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
149 | - } |
|
148 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Fetch all records with relations from storage in pages. |
|
153 | - * |
|
154 | - * @param integer $perPage |
|
155 | - * @param array $relations |
|
156 | - * @param string $sortBy |
|
157 | - * @param boolean $desc |
|
158 | - * @param array $columns |
|
159 | - * @return collection |
|
160 | - */ |
|
161 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
162 | - { |
|
163 | - $sort = $desc ? 'desc' : 'asc'; |
|
164 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Fetch all records with relations based on |
|
169 | - * the given condition from storage in pages. |
|
170 | - * |
|
171 | - * @param array $conditions array of conditions |
|
172 | - * @param integer $perPage |
|
173 | - * @param array $relations |
|
174 | - * @param string $sortBy |
|
175 | - * @param boolean $desc |
|
176 | - * @param array $columns |
|
177 | - * @return collection |
|
178 | - */ |
|
179 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
180 | - { |
|
181 | - unset($conditions['page']); |
|
182 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
183 | - $sort = $desc ? 'desc' : 'asc'; |
|
184 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
185 | - } |
|
151 | + /** |
|
152 | + * Fetch all records with relations from storage in pages. |
|
153 | + * |
|
154 | + * @param integer $perPage |
|
155 | + * @param array $relations |
|
156 | + * @param string $sortBy |
|
157 | + * @param boolean $desc |
|
158 | + * @param array $columns |
|
159 | + * @return collection |
|
160 | + */ |
|
161 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
162 | + { |
|
163 | + $sort = $desc ? 'desc' : 'asc'; |
|
164 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Fetch all records with relations based on |
|
169 | + * the given condition from storage in pages. |
|
170 | + * |
|
171 | + * @param array $conditions array of conditions |
|
172 | + * @param integer $perPage |
|
173 | + * @param array $relations |
|
174 | + * @param string $sortBy |
|
175 | + * @param boolean $desc |
|
176 | + * @param array $columns |
|
177 | + * @return collection |
|
178 | + */ |
|
179 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
180 | + { |
|
181 | + unset($conditions['page']); |
|
182 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
183 | + $sort = $desc ? 'desc' : 'asc'; |
|
184 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Save the given model to the storage. |
|
189 | - * |
|
190 | - * @param array $data |
|
191 | - * @return mixed |
|
192 | - */ |
|
193 | - public function save(array $data) |
|
194 | - { |
|
195 | - \Session::put('locale', 'all'); |
|
196 | - $model = false; |
|
197 | - $modelClass = $this->model; |
|
198 | - $relations = []; |
|
199 | - |
|
200 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
201 | - /** |
|
202 | - * If the id is present in the data then select the model for updating, |
|
203 | - * else create new model. |
|
204 | - * @var array |
|
205 | - */ |
|
206 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
207 | - if (! $model) { |
|
208 | - \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Construct the model object with the given data, |
|
213 | - * and if there is a relation add it to relations array, |
|
214 | - * then save the model. |
|
215 | - */ |
|
216 | - foreach ($data as $key => $value) { |
|
217 | - /** |
|
218 | - * If the attribute is a relation. |
|
219 | - */ |
|
220 | - $relation = camel_case($key); |
|
221 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
222 | - /** |
|
223 | - * Check if the relation is a collection. |
|
224 | - */ |
|
225 | - if (class_basename($model->$relation) == 'Collection') { |
|
226 | - /** |
|
227 | - * If the relation has no value then marke the relation data |
|
228 | - * related to the model to be deleted. |
|
229 | - */ |
|
230 | - if (! $value || ! count($value)) { |
|
231 | - $relations[$relation] = 'delete'; |
|
232 | - } |
|
233 | - } |
|
234 | - if (is_array($value)) { |
|
235 | - /** |
|
236 | - * Loop through the relation data. |
|
237 | - */ |
|
238 | - foreach ($value as $attr => $val) { |
|
239 | - /** |
|
240 | - * Get the relation model. |
|
241 | - */ |
|
242 | - $relationBaseModel = \Core::$relation()->model; |
|
243 | - |
|
244 | - /** |
|
245 | - * Check if the relation is a collection. |
|
246 | - */ |
|
247 | - if (class_basename($model->$relation) == 'Collection') { |
|
248 | - /** |
|
249 | - * If the id is present in the data then select the relation model for updating, |
|
250 | - * else create new model. |
|
251 | - */ |
|
252 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
253 | - |
|
254 | - /** |
|
255 | - * If model doesn't exists. |
|
256 | - */ |
|
257 | - if (! $relationModel) { |
|
258 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Loop through the relation attributes. |
|
263 | - */ |
|
264 | - foreach ($val as $attr => $val) { |
|
265 | - /** |
|
266 | - * Prevent the sub relations or attributes not in the fillable. |
|
267 | - */ |
|
268 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
269 | - $relationModel->$attr = $val; |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - $relations[$relation][] = $relationModel; |
|
274 | - } else { |
|
275 | - /** |
|
276 | - * Prevent the sub relations. |
|
277 | - */ |
|
278 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
279 | - /** |
|
280 | - * If the id is present in the data then select the relation model for updating, |
|
281 | - * else create new model. |
|
282 | - */ |
|
283 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
284 | - |
|
285 | - /** |
|
286 | - * If model doesn't exists. |
|
287 | - */ |
|
288 | - if (! $relationModel) { |
|
289 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
290 | - } |
|
291 | - |
|
292 | - foreach ($value as $relationAttribute => $relationValue) { |
|
293 | - /** |
|
294 | - * Prevent attributes not in the fillable. |
|
295 | - */ |
|
296 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
297 | - $relationModel->$relationAttribute = $relationValue; |
|
298 | - } |
|
299 | - } |
|
300 | - |
|
301 | - $relations[$relation] = $relationModel; |
|
302 | - } |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
306 | - } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
307 | - /** |
|
308 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
309 | - */ |
|
310 | - $model->$key = $value; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Loop through the relations array. |
|
316 | - */ |
|
317 | - foreach ($relations as $key => $value) { |
|
318 | - /** |
|
319 | - * If the relation is marked for delete then delete it. |
|
320 | - */ |
|
321 | - if ($value == 'delete' && $model->$key()->count()) { |
|
322 | - $model->$key()->delete(); |
|
323 | - } elseif (gettype($value) == 'array') { |
|
324 | - /** |
|
325 | - * Save the model. |
|
326 | - */ |
|
327 | - $model->save(); |
|
328 | - $ids = []; |
|
329 | - |
|
330 | - /** |
|
331 | - * Loop through the relations. |
|
332 | - */ |
|
333 | - foreach ($value as $val) { |
|
334 | - switch (class_basename($model->$key())) { |
|
335 | - /** |
|
336 | - * If the relation is one to many then update it's foreign key with |
|
337 | - * the model id and save it then add its id to ids array to delete all |
|
338 | - * relations who's id isn't in the ids array. |
|
339 | - */ |
|
340 | - case 'HasMany': |
|
341 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
342 | - $val->$foreignKeyName = $model->id; |
|
343 | - $val->save(); |
|
344 | - $ids[] = $val->id; |
|
345 | - break; |
|
346 | - |
|
347 | - /** |
|
348 | - * If the relation is many to many then add it's id to the ids array to |
|
349 | - * attache these ids to the model. |
|
350 | - */ |
|
351 | - case 'BelongsToMany': |
|
352 | - $val->save(); |
|
353 | - $ids[] = $val->id; |
|
354 | - break; |
|
355 | - } |
|
356 | - } |
|
357 | - switch (class_basename($model->$key())) { |
|
358 | - /** |
|
359 | - * If the relation is one to many then delete all |
|
360 | - * relations who's id isn't in the ids array. |
|
361 | - */ |
|
362 | - case 'HasMany': |
|
363 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
364 | - break; |
|
365 | - |
|
366 | - /** |
|
367 | - * If the relation is many to many then |
|
368 | - * detach the previous data and attach |
|
369 | - * the ids array to the model. |
|
370 | - */ |
|
371 | - case 'BelongsToMany': |
|
372 | - $model->$key()->detach(); |
|
373 | - $model->$key()->attach($ids); |
|
374 | - break; |
|
375 | - } |
|
376 | - } else { |
|
377 | - switch (class_basename($model->$key())) { |
|
378 | - /** |
|
379 | - * If the relation is one to one. |
|
380 | - */ |
|
381 | - case 'HasOne': |
|
382 | - /** |
|
383 | - * Save the model. |
|
384 | - */ |
|
385 | - $model->save(); |
|
386 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
387 | - $value->$foreignKeyName = $model->id; |
|
388 | - $value->save(); |
|
389 | - break; |
|
390 | - case 'BelongsTo': |
|
391 | - /** |
|
392 | - * Save the model. |
|
393 | - */ |
|
394 | - $value->save(); |
|
395 | - $model->$key()->associate($value); |
|
396 | - break; |
|
397 | - } |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Save the model. |
|
403 | - */ |
|
404 | - $model->save(); |
|
405 | - }); |
|
187 | + /** |
|
188 | + * Save the given model to the storage. |
|
189 | + * |
|
190 | + * @param array $data |
|
191 | + * @return mixed |
|
192 | + */ |
|
193 | + public function save(array $data) |
|
194 | + { |
|
195 | + \Session::put('locale', 'all'); |
|
196 | + $model = false; |
|
197 | + $modelClass = $this->model; |
|
198 | + $relations = []; |
|
199 | + |
|
200 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
201 | + /** |
|
202 | + * If the id is present in the data then select the model for updating, |
|
203 | + * else create new model. |
|
204 | + * @var array |
|
205 | + */ |
|
206 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
207 | + if (! $model) { |
|
208 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Construct the model object with the given data, |
|
213 | + * and if there is a relation add it to relations array, |
|
214 | + * then save the model. |
|
215 | + */ |
|
216 | + foreach ($data as $key => $value) { |
|
217 | + /** |
|
218 | + * If the attribute is a relation. |
|
219 | + */ |
|
220 | + $relation = camel_case($key); |
|
221 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
222 | + /** |
|
223 | + * Check if the relation is a collection. |
|
224 | + */ |
|
225 | + if (class_basename($model->$relation) == 'Collection') { |
|
226 | + /** |
|
227 | + * If the relation has no value then marke the relation data |
|
228 | + * related to the model to be deleted. |
|
229 | + */ |
|
230 | + if (! $value || ! count($value)) { |
|
231 | + $relations[$relation] = 'delete'; |
|
232 | + } |
|
233 | + } |
|
234 | + if (is_array($value)) { |
|
235 | + /** |
|
236 | + * Loop through the relation data. |
|
237 | + */ |
|
238 | + foreach ($value as $attr => $val) { |
|
239 | + /** |
|
240 | + * Get the relation model. |
|
241 | + */ |
|
242 | + $relationBaseModel = \Core::$relation()->model; |
|
243 | + |
|
244 | + /** |
|
245 | + * Check if the relation is a collection. |
|
246 | + */ |
|
247 | + if (class_basename($model->$relation) == 'Collection') { |
|
248 | + /** |
|
249 | + * If the id is present in the data then select the relation model for updating, |
|
250 | + * else create new model. |
|
251 | + */ |
|
252 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
253 | + |
|
254 | + /** |
|
255 | + * If model doesn't exists. |
|
256 | + */ |
|
257 | + if (! $relationModel) { |
|
258 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Loop through the relation attributes. |
|
263 | + */ |
|
264 | + foreach ($val as $attr => $val) { |
|
265 | + /** |
|
266 | + * Prevent the sub relations or attributes not in the fillable. |
|
267 | + */ |
|
268 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
269 | + $relationModel->$attr = $val; |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + $relations[$relation][] = $relationModel; |
|
274 | + } else { |
|
275 | + /** |
|
276 | + * Prevent the sub relations. |
|
277 | + */ |
|
278 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
279 | + /** |
|
280 | + * If the id is present in the data then select the relation model for updating, |
|
281 | + * else create new model. |
|
282 | + */ |
|
283 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
284 | + |
|
285 | + /** |
|
286 | + * If model doesn't exists. |
|
287 | + */ |
|
288 | + if (! $relationModel) { |
|
289 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
290 | + } |
|
291 | + |
|
292 | + foreach ($value as $relationAttribute => $relationValue) { |
|
293 | + /** |
|
294 | + * Prevent attributes not in the fillable. |
|
295 | + */ |
|
296 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
297 | + $relationModel->$relationAttribute = $relationValue; |
|
298 | + } |
|
299 | + } |
|
300 | + |
|
301 | + $relations[$relation] = $relationModel; |
|
302 | + } |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | + } elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
307 | + /** |
|
308 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
309 | + */ |
|
310 | + $model->$key = $value; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Loop through the relations array. |
|
316 | + */ |
|
317 | + foreach ($relations as $key => $value) { |
|
318 | + /** |
|
319 | + * If the relation is marked for delete then delete it. |
|
320 | + */ |
|
321 | + if ($value == 'delete' && $model->$key()->count()) { |
|
322 | + $model->$key()->delete(); |
|
323 | + } elseif (gettype($value) == 'array') { |
|
324 | + /** |
|
325 | + * Save the model. |
|
326 | + */ |
|
327 | + $model->save(); |
|
328 | + $ids = []; |
|
329 | + |
|
330 | + /** |
|
331 | + * Loop through the relations. |
|
332 | + */ |
|
333 | + foreach ($value as $val) { |
|
334 | + switch (class_basename($model->$key())) { |
|
335 | + /** |
|
336 | + * If the relation is one to many then update it's foreign key with |
|
337 | + * the model id and save it then add its id to ids array to delete all |
|
338 | + * relations who's id isn't in the ids array. |
|
339 | + */ |
|
340 | + case 'HasMany': |
|
341 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
342 | + $val->$foreignKeyName = $model->id; |
|
343 | + $val->save(); |
|
344 | + $ids[] = $val->id; |
|
345 | + break; |
|
346 | + |
|
347 | + /** |
|
348 | + * If the relation is many to many then add it's id to the ids array to |
|
349 | + * attache these ids to the model. |
|
350 | + */ |
|
351 | + case 'BelongsToMany': |
|
352 | + $val->save(); |
|
353 | + $ids[] = $val->id; |
|
354 | + break; |
|
355 | + } |
|
356 | + } |
|
357 | + switch (class_basename($model->$key())) { |
|
358 | + /** |
|
359 | + * If the relation is one to many then delete all |
|
360 | + * relations who's id isn't in the ids array. |
|
361 | + */ |
|
362 | + case 'HasMany': |
|
363 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
364 | + break; |
|
365 | + |
|
366 | + /** |
|
367 | + * If the relation is many to many then |
|
368 | + * detach the previous data and attach |
|
369 | + * the ids array to the model. |
|
370 | + */ |
|
371 | + case 'BelongsToMany': |
|
372 | + $model->$key()->detach(); |
|
373 | + $model->$key()->attach($ids); |
|
374 | + break; |
|
375 | + } |
|
376 | + } else { |
|
377 | + switch (class_basename($model->$key())) { |
|
378 | + /** |
|
379 | + * If the relation is one to one. |
|
380 | + */ |
|
381 | + case 'HasOne': |
|
382 | + /** |
|
383 | + * Save the model. |
|
384 | + */ |
|
385 | + $model->save(); |
|
386 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
387 | + $value->$foreignKeyName = $model->id; |
|
388 | + $value->save(); |
|
389 | + break; |
|
390 | + case 'BelongsTo': |
|
391 | + /** |
|
392 | + * Save the model. |
|
393 | + */ |
|
394 | + $value->save(); |
|
395 | + $model->$key()->associate($value); |
|
396 | + break; |
|
397 | + } |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Save the model. |
|
403 | + */ |
|
404 | + $model->save(); |
|
405 | + }); |
|
406 | 406 | |
407 | - return $model; |
|
408 | - } |
|
407 | + return $model; |
|
408 | + } |
|
409 | 409 | |
410 | - /** |
|
411 | - * Update record in the storage based on the given |
|
412 | - * condition. |
|
413 | - * |
|
414 | - * @param var $value condition value |
|
415 | - * @param array $data |
|
416 | - * @param string $attribute condition column name |
|
417 | - * @return void |
|
418 | - */ |
|
419 | - public function update($value, array $data, $attribute = 'id') |
|
420 | - { |
|
421 | - if ($attribute == 'id') { |
|
422 | - $model = $this->model->lockForUpdate()->find($value); |
|
423 | - $model ? $model->update($data) : 0; |
|
424 | - } else { |
|
425 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
426 | - $model->update($data); |
|
427 | - }); |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Delete record from the storage based on the given |
|
433 | - * condition. |
|
434 | - * |
|
435 | - * @param var $value condition value |
|
436 | - * @param string $attribute condition column name |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - public function delete($value, $attribute = 'id') |
|
440 | - { |
|
441 | - if ($attribute == 'id') { |
|
442 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
443 | - $model = $this->model->lockForUpdate()->find($value); |
|
444 | - if (! $model) { |
|
445 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
446 | - } |
|
410 | + /** |
|
411 | + * Update record in the storage based on the given |
|
412 | + * condition. |
|
413 | + * |
|
414 | + * @param var $value condition value |
|
415 | + * @param array $data |
|
416 | + * @param string $attribute condition column name |
|
417 | + * @return void |
|
418 | + */ |
|
419 | + public function update($value, array $data, $attribute = 'id') |
|
420 | + { |
|
421 | + if ($attribute == 'id') { |
|
422 | + $model = $this->model->lockForUpdate()->find($value); |
|
423 | + $model ? $model->update($data) : 0; |
|
424 | + } else { |
|
425 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
426 | + $model->update($data); |
|
427 | + }); |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Delete record from the storage based on the given |
|
433 | + * condition. |
|
434 | + * |
|
435 | + * @param var $value condition value |
|
436 | + * @param string $attribute condition column name |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + public function delete($value, $attribute = 'id') |
|
440 | + { |
|
441 | + if ($attribute == 'id') { |
|
442 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
443 | + $model = $this->model->lockForUpdate()->find($value); |
|
444 | + if (! $model) { |
|
445 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
446 | + } |
|
447 | 447 | |
448 | - $model->delete(); |
|
449 | - }); |
|
450 | - } else { |
|
451 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
452 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) { |
|
453 | - $model->delete(); |
|
454 | - }); |
|
455 | - }); |
|
456 | - } |
|
457 | - } |
|
448 | + $model->delete(); |
|
449 | + }); |
|
450 | + } else { |
|
451 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
452 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) { |
|
453 | + $model->delete(); |
|
454 | + }); |
|
455 | + }); |
|
456 | + } |
|
457 | + } |
|
458 | 458 | |
459 | - /** |
|
460 | - * Fetch records from the storage based on the given |
|
461 | - * id. |
|
462 | - * |
|
463 | - * @param integer $id |
|
464 | - * @param string[] $relations |
|
465 | - * @param array $columns |
|
466 | - * @return object |
|
467 | - */ |
|
468 | - public function find($id, $relations = [], $columns = ['*']) |
|
469 | - { |
|
470 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
471 | - } |
|
459 | + /** |
|
460 | + * Fetch records from the storage based on the given |
|
461 | + * id. |
|
462 | + * |
|
463 | + * @param integer $id |
|
464 | + * @param string[] $relations |
|
465 | + * @param array $columns |
|
466 | + * @return object |
|
467 | + */ |
|
468 | + public function find($id, $relations = [], $columns = ['*']) |
|
469 | + { |
|
470 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
471 | + } |
|
472 | 472 | |
473 | - /** |
|
474 | - * Fetch records from the storage based on the given |
|
475 | - * condition. |
|
476 | - * |
|
477 | - * @param array $conditions array of conditions |
|
478 | - * @param array $relations |
|
479 | - * @param string $sortBy |
|
480 | - * @param boolean $desc |
|
481 | - * @param array $columns |
|
482 | - * @return collection |
|
483 | - */ |
|
484 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
485 | - { |
|
486 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
487 | - $sort = $desc ? 'desc' : 'asc'; |
|
488 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Fetch the first record from the storage based on the given |
|
493 | - * condition. |
|
494 | - * |
|
495 | - * @param array $conditions array of conditions |
|
496 | - * @param array $relations |
|
497 | - * @param array $columns |
|
498 | - * @return object |
|
499 | - */ |
|
500 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
501 | - { |
|
502 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
503 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Return the deleted models in pages based on the given conditions. |
|
508 | - * |
|
509 | - * @param array $conditions array of conditions |
|
510 | - * @param integer $perPage |
|
511 | - * @param string $sortBy |
|
512 | - * @param boolean $desc |
|
513 | - * @param array $columns |
|
514 | - * @return collection |
|
515 | - */ |
|
516 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
517 | - { |
|
518 | - unset($conditions['page']); |
|
519 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
520 | - $sort = $desc ? 'desc' : 'asc'; |
|
521 | - $model = $this->model->onlyTrashed(); |
|
522 | - |
|
523 | - if (count($conditions['conditionValues'])) { |
|
524 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
525 | - } |
|
526 | - |
|
527 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Restore the deleted model. |
|
532 | - * |
|
533 | - * @param integer $id |
|
534 | - * @return void |
|
535 | - */ |
|
536 | - public function restore($id) |
|
537 | - { |
|
538 | - $model = $this->model->onlyTrashed()->find($id); |
|
539 | - |
|
540 | - if (! $model) { |
|
541 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
542 | - } |
|
543 | - |
|
544 | - $model->restore(); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Build the conditions recursively for the retrieving methods. |
|
549 | - * @param array $conditions |
|
550 | - * @return array |
|
551 | - */ |
|
552 | - protected function constructConditions($conditions, $model) |
|
553 | - { |
|
554 | - $conditionString = ''; |
|
555 | - $conditionValues = []; |
|
556 | - foreach ($conditions as $key => $value) { |
|
557 | - if (Str::contains($key, '->')) { |
|
558 | - $key = $this->wrapJsonSelector($key); |
|
559 | - } |
|
560 | - |
|
561 | - if ($key == 'and') { |
|
562 | - $conditions = $this->constructConditions($value, $model); |
|
563 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
564 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
565 | - } elseif ($key == 'or') { |
|
566 | - $conditions = $this->constructConditions($value, $model); |
|
567 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
568 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
569 | - } else { |
|
570 | - if (is_array($value)) { |
|
571 | - $operator = $value['op']; |
|
572 | - if (strtolower($operator) == 'between') { |
|
573 | - $value1 = $value['val1']; |
|
574 | - $value2 = $value['val2']; |
|
575 | - } else { |
|
576 | - $value = Arr::get($value, 'val', ''); |
|
577 | - } |
|
578 | - } else { |
|
579 | - $operator = '='; |
|
580 | - } |
|
473 | + /** |
|
474 | + * Fetch records from the storage based on the given |
|
475 | + * condition. |
|
476 | + * |
|
477 | + * @param array $conditions array of conditions |
|
478 | + * @param array $relations |
|
479 | + * @param string $sortBy |
|
480 | + * @param boolean $desc |
|
481 | + * @param array $columns |
|
482 | + * @return collection |
|
483 | + */ |
|
484 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
485 | + { |
|
486 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
487 | + $sort = $desc ? 'desc' : 'asc'; |
|
488 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Fetch the first record from the storage based on the given |
|
493 | + * condition. |
|
494 | + * |
|
495 | + * @param array $conditions array of conditions |
|
496 | + * @param array $relations |
|
497 | + * @param array $columns |
|
498 | + * @return object |
|
499 | + */ |
|
500 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
501 | + { |
|
502 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
503 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Return the deleted models in pages based on the given conditions. |
|
508 | + * |
|
509 | + * @param array $conditions array of conditions |
|
510 | + * @param integer $perPage |
|
511 | + * @param string $sortBy |
|
512 | + * @param boolean $desc |
|
513 | + * @param array $columns |
|
514 | + * @return collection |
|
515 | + */ |
|
516 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
517 | + { |
|
518 | + unset($conditions['page']); |
|
519 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
520 | + $sort = $desc ? 'desc' : 'asc'; |
|
521 | + $model = $this->model->onlyTrashed(); |
|
522 | + |
|
523 | + if (count($conditions['conditionValues'])) { |
|
524 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
525 | + } |
|
526 | + |
|
527 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Restore the deleted model. |
|
532 | + * |
|
533 | + * @param integer $id |
|
534 | + * @return void |
|
535 | + */ |
|
536 | + public function restore($id) |
|
537 | + { |
|
538 | + $model = $this->model->onlyTrashed()->find($id); |
|
539 | + |
|
540 | + if (! $model) { |
|
541 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
542 | + } |
|
543 | + |
|
544 | + $model->restore(); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Build the conditions recursively for the retrieving methods. |
|
549 | + * @param array $conditions |
|
550 | + * @return array |
|
551 | + */ |
|
552 | + protected function constructConditions($conditions, $model) |
|
553 | + { |
|
554 | + $conditionString = ''; |
|
555 | + $conditionValues = []; |
|
556 | + foreach ($conditions as $key => $value) { |
|
557 | + if (Str::contains($key, '->')) { |
|
558 | + $key = $this->wrapJsonSelector($key); |
|
559 | + } |
|
560 | + |
|
561 | + if ($key == 'and') { |
|
562 | + $conditions = $this->constructConditions($value, $model); |
|
563 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
564 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
565 | + } elseif ($key == 'or') { |
|
566 | + $conditions = $this->constructConditions($value, $model); |
|
567 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
568 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
569 | + } else { |
|
570 | + if (is_array($value)) { |
|
571 | + $operator = $value['op']; |
|
572 | + if (strtolower($operator) == 'between') { |
|
573 | + $value1 = $value['val1']; |
|
574 | + $value2 = $value['val2']; |
|
575 | + } else { |
|
576 | + $value = Arr::get($value, 'val', ''); |
|
577 | + } |
|
578 | + } else { |
|
579 | + $operator = '='; |
|
580 | + } |
|
581 | 581 | |
582 | - if (strtolower($operator) == 'between') { |
|
583 | - $conditionString .= $key.' >= ? and '; |
|
584 | - $conditionValues[] = $value1; |
|
585 | - |
|
586 | - $conditionString .= $key.' <= ? {op} '; |
|
587 | - $conditionValues[] = $value2; |
|
588 | - } elseif (strtolower($operator) == 'in') { |
|
589 | - $conditionValues = array_merge($conditionValues, $value); |
|
590 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
591 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
592 | - } elseif (strtolower($operator) == 'null') { |
|
593 | - $conditionString .= $key.' is null {op} '; |
|
594 | - } elseif (strtolower($operator) == 'not null') { |
|
595 | - $conditionString .= $key.' is not null {op} '; |
|
596 | - } elseif (strtolower($operator) == 'has') { |
|
597 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
598 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
599 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
600 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
601 | - } else { |
|
602 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
603 | - $conditionValues[] = $value; |
|
604 | - } |
|
605 | - } |
|
606 | - } |
|
607 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
608 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * Wrap the given JSON selector. |
|
613 | - * |
|
614 | - * @param string $value |
|
615 | - * @return string |
|
616 | - */ |
|
617 | - protected function wrapJsonSelector($value) |
|
618 | - { |
|
619 | - $removeLast = strpos($value, ')'); |
|
620 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
621 | - $path = explode('->', $value); |
|
622 | - $field = array_shift($path); |
|
623 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
624 | - return '"'.$part.'"'; |
|
625 | - })->implode('.')); |
|
582 | + if (strtolower($operator) == 'between') { |
|
583 | + $conditionString .= $key.' >= ? and '; |
|
584 | + $conditionValues[] = $value1; |
|
585 | + |
|
586 | + $conditionString .= $key.' <= ? {op} '; |
|
587 | + $conditionValues[] = $value2; |
|
588 | + } elseif (strtolower($operator) == 'in') { |
|
589 | + $conditionValues = array_merge($conditionValues, $value); |
|
590 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
591 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
592 | + } elseif (strtolower($operator) == 'null') { |
|
593 | + $conditionString .= $key.' is null {op} '; |
|
594 | + } elseif (strtolower($operator) == 'not null') { |
|
595 | + $conditionString .= $key.' is not null {op} '; |
|
596 | + } elseif (strtolower($operator) == 'has') { |
|
597 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
598 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
599 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
600 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
601 | + } else { |
|
602 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
603 | + $conditionValues[] = $value; |
|
604 | + } |
|
605 | + } |
|
606 | + } |
|
607 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
608 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * Wrap the given JSON selector. |
|
613 | + * |
|
614 | + * @param string $value |
|
615 | + * @return string |
|
616 | + */ |
|
617 | + protected function wrapJsonSelector($value) |
|
618 | + { |
|
619 | + $removeLast = strpos($value, ')'); |
|
620 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
621 | + $path = explode('->', $value); |
|
622 | + $field = array_shift($path); |
|
623 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
624 | + return '"'.$part.'"'; |
|
625 | + })->implode('.')); |
|
626 | 626 | |
627 | - return $removeLast === false ? $result : $result.')'; |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Abstract method that return the necessary |
|
632 | - * information (full model namespace) |
|
633 | - * needed to preform the previous actions. |
|
634 | - * |
|
635 | - * @return string |
|
636 | - */ |
|
637 | - abstract protected function getModel(); |
|
627 | + return $removeLast === false ? $result : $result.')'; |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Abstract method that return the necessary |
|
632 | + * information (full model namespace) |
|
633 | + * needed to preform the previous actions. |
|
634 | + * |
|
635 | + * @return string |
|
636 | + */ |
|
637 | + abstract protected function getModel(); |
|
638 | 638 | } |
@@ -6,28 +6,28 @@ |
||
6 | 6 | |
7 | 7 | class OauthClient extends Client |
8 | 8 | { |
9 | - protected $dates = ['created_at', 'updated_at']; |
|
10 | - protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
11 | - public $searchable = ['name']; |
|
9 | + protected $dates = ['created_at', 'updated_at']; |
|
10 | + protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
11 | + public $searchable = ['name']; |
|
12 | 12 | |
13 | - public function getCreatedAtAttribute($value) |
|
14 | - { |
|
15 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
16 | - } |
|
13 | + public function getCreatedAtAttribute($value) |
|
14 | + { |
|
15 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
16 | + } |
|
17 | 17 | |
18 | - public function getUpdatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getUpdatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function user() |
|
24 | - { |
|
25 | - return $this->belongsTo('App\Modules\Acl\AclUser'); |
|
26 | - } |
|
23 | + public function user() |
|
24 | + { |
|
25 | + return $this->belongsTo('App\Modules\Acl\AclUser'); |
|
26 | + } |
|
27 | 27 | |
28 | - public static function boot() |
|
29 | - { |
|
30 | - parent::boot(); |
|
31 | - OauthClient::observe(\App::make('App\Modules\Acl\ModelObservers\OauthClientObserver')); |
|
32 | - } |
|
28 | + public static function boot() |
|
29 | + { |
|
30 | + parent::boot(); |
|
31 | + OauthClient::observe(\App::make('App\Modules\Acl\ModelObservers\OauthClientObserver')); |
|
32 | + } |
|
33 | 33 | } |
@@ -7,20 +7,20 @@ |
||
7 | 7 | |
8 | 8 | class AclPermission extends JsonResource |
9 | 9 | { |
10 | - /** |
|
11 | - * Transform the resource into an array. |
|
12 | - * |
|
13 | - * @param \Illuminate\Http\Request $request |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function toArray($request) |
|
17 | - { |
|
18 | - return [ |
|
19 | - 'id' => $this->id, |
|
20 | - 'name' => $this->name, |
|
21 | - 'groups' => GroupResource::collection($this->whenLoaded('groups')), |
|
22 | - 'createdAt' => $this->created_at, |
|
23 | - 'updatedAt' => $this->updated_at, |
|
24 | - ]; |
|
25 | - } |
|
10 | + /** |
|
11 | + * Transform the resource into an array. |
|
12 | + * |
|
13 | + * @param \Illuminate\Http\Request $request |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function toArray($request) |
|
17 | + { |
|
18 | + return [ |
|
19 | + 'id' => $this->id, |
|
20 | + 'name' => $this->name, |
|
21 | + 'groups' => GroupResource::collection($this->whenLoaded('groups')), |
|
22 | + 'createdAt' => $this->created_at, |
|
23 | + 'updatedAt' => $this->updated_at, |
|
24 | + ]; |
|
25 | + } |
|
26 | 26 | } |