@@ -7,250 +7,250 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class BaseService implements BaseServiceInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var object |
|
| 12 | - */ |
|
| 13 | - public $repo; |
|
| 10 | + /** |
|
| 11 | + * @var object |
|
| 12 | + */ |
|
| 13 | + public $repo; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Init new object. |
|
| 17 | - * |
|
| 18 | - * @param mixed $repo |
|
| 19 | - * @return void |
|
| 20 | - */ |
|
| 21 | - public function __construct($repo) |
|
| 22 | - { |
|
| 23 | - $this->repo = $repo; |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Init new object. |
|
| 17 | + * |
|
| 18 | + * @param mixed $repo |
|
| 19 | + * @return void |
|
| 20 | + */ |
|
| 21 | + public function __construct($repo) |
|
| 22 | + { |
|
| 23 | + $this->repo = $repo; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Fetch records with relations based on the given params. |
|
| 28 | - * |
|
| 29 | - * @param array $relations |
|
| 30 | - * @param array $conditions |
|
| 31 | - * @param integer $perPage |
|
| 32 | - * @param string $sortBy |
|
| 33 | - * @param boolean $desc |
|
| 34 | - * @param boolean $trashed |
|
| 35 | - * @return collection |
|
| 36 | - */ |
|
| 37 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 38 | - { |
|
| 39 | - $translatable = $this->repo->model->translatable ?? []; |
|
| 40 | - $filters = $this->constructFilters($conditions); |
|
| 41 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . Session::get('locale') : $sortBy; |
|
| 26 | + /** |
|
| 27 | + * Fetch records with relations based on the given params. |
|
| 28 | + * |
|
| 29 | + * @param array $relations |
|
| 30 | + * @param array $conditions |
|
| 31 | + * @param integer $perPage |
|
| 32 | + * @param string $sortBy |
|
| 33 | + * @param boolean $desc |
|
| 34 | + * @param boolean $trashed |
|
| 35 | + * @return collection |
|
| 36 | + */ |
|
| 37 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 38 | + { |
|
| 39 | + $translatable = $this->repo->model->translatable ?? []; |
|
| 40 | + $filters = $this->constructFilters($conditions); |
|
| 41 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . Session::get('locale') : $sortBy; |
|
| 42 | 42 | |
| 43 | - if ($trashed) { |
|
| 44 | - return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 45 | - } |
|
| 43 | + if ($trashed) { |
|
| 44 | + return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if (count($filters)) { |
|
| 48 | - return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 49 | - } |
|
| 47 | + if (count($filters)) { |
|
| 48 | + return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 52 | - } |
|
| 51 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Fetch all records with relations from the storage. |
|
| 56 | - * |
|
| 57 | - * @param array $relations |
|
| 58 | - * @param string $sortBy |
|
| 59 | - * @param boolean $desc |
|
| 60 | - * @param array $columns |
|
| 61 | - * @return collection |
|
| 62 | - */ |
|
| 63 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 64 | - { |
|
| 65 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 66 | - } |
|
| 54 | + /** |
|
| 55 | + * Fetch all records with relations from the storage. |
|
| 56 | + * |
|
| 57 | + * @param array $relations |
|
| 58 | + * @param string $sortBy |
|
| 59 | + * @param boolean $desc |
|
| 60 | + * @param array $columns |
|
| 61 | + * @return collection |
|
| 62 | + */ |
|
| 63 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 64 | + { |
|
| 65 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Fetch all records with relations from storage in pages. |
|
| 70 | - * |
|
| 71 | - * @param integer $perPage |
|
| 72 | - * @param array $relations |
|
| 73 | - * @param string $sortBy |
|
| 74 | - * @param boolean $desc |
|
| 75 | - * @param array $columns |
|
| 76 | - * @return collection |
|
| 77 | - */ |
|
| 78 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 79 | - { |
|
| 80 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * Fetch all records with relations from storage in pages. |
|
| 70 | + * |
|
| 71 | + * @param integer $perPage |
|
| 72 | + * @param array $relations |
|
| 73 | + * @param string $sortBy |
|
| 74 | + * @param boolean $desc |
|
| 75 | + * @param array $columns |
|
| 76 | + * @return collection |
|
| 77 | + */ |
|
| 78 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 79 | + { |
|
| 80 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Fetch all records with relations based on |
|
| 85 | - * the given condition from storage in pages. |
|
| 86 | - * |
|
| 87 | - * @param array $conditions array of conditions |
|
| 88 | - * @param integer $perPage |
|
| 89 | - * @param array $relations |
|
| 90 | - * @param string $sortBy |
|
| 91 | - * @param boolean $desc |
|
| 92 | - * @param array $columns |
|
| 93 | - * @return collection |
|
| 94 | - */ |
|
| 95 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 96 | - { |
|
| 97 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 98 | - } |
|
| 83 | + /** |
|
| 84 | + * Fetch all records with relations based on |
|
| 85 | + * the given condition from storage in pages. |
|
| 86 | + * |
|
| 87 | + * @param array $conditions array of conditions |
|
| 88 | + * @param integer $perPage |
|
| 89 | + * @param array $relations |
|
| 90 | + * @param string $sortBy |
|
| 91 | + * @param boolean $desc |
|
| 92 | + * @param array $columns |
|
| 93 | + * @return collection |
|
| 94 | + */ |
|
| 95 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 96 | + { |
|
| 97 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Save the given model to the storage. |
|
| 102 | - * |
|
| 103 | - * @param array $data |
|
| 104 | - * @return mixed |
|
| 105 | - */ |
|
| 106 | - public function save(array $data) |
|
| 107 | - { |
|
| 108 | - return $this->repo->save($data); |
|
| 109 | - } |
|
| 100 | + /** |
|
| 101 | + * Save the given model to the storage. |
|
| 102 | + * |
|
| 103 | + * @param array $data |
|
| 104 | + * @return mixed |
|
| 105 | + */ |
|
| 106 | + public function save(array $data) |
|
| 107 | + { |
|
| 108 | + return $this->repo->save($data); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Delete record from the storage based on the given |
|
| 113 | - * condition. |
|
| 114 | - * |
|
| 115 | - * @param var $value condition value |
|
| 116 | - * @param string $attribute condition column name |
|
| 117 | - * @return void |
|
| 118 | - */ |
|
| 119 | - public function delete($value, $attribute = 'id') |
|
| 120 | - { |
|
| 121 | - return $this->repo->delete($value, $attribute); |
|
| 122 | - } |
|
| 111 | + /** |
|
| 112 | + * Delete record from the storage based on the given |
|
| 113 | + * condition. |
|
| 114 | + * |
|
| 115 | + * @param var $value condition value |
|
| 116 | + * @param string $attribute condition column name |
|
| 117 | + * @return void |
|
| 118 | + */ |
|
| 119 | + public function delete($value, $attribute = 'id') |
|
| 120 | + { |
|
| 121 | + return $this->repo->delete($value, $attribute); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Fetch records from the storage based on the given |
|
| 126 | - * id. |
|
| 127 | - * |
|
| 128 | - * @param integer $id |
|
| 129 | - * @param array $relations |
|
| 130 | - * @param array $columns |
|
| 131 | - * @return object |
|
| 132 | - */ |
|
| 133 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 134 | - { |
|
| 135 | - return $this->repo->find($id, $relations, $columns); |
|
| 136 | - } |
|
| 124 | + /** |
|
| 125 | + * Fetch records from the storage based on the given |
|
| 126 | + * id. |
|
| 127 | + * |
|
| 128 | + * @param integer $id |
|
| 129 | + * @param array $relations |
|
| 130 | + * @param array $columns |
|
| 131 | + * @return object |
|
| 132 | + */ |
|
| 133 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 134 | + { |
|
| 135 | + return $this->repo->find($id, $relations, $columns); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Fetch records from the storage based on the given |
|
| 140 | - * condition. |
|
| 141 | - * |
|
| 142 | - * @param array $conditions array of conditions |
|
| 143 | - * @param array $relations |
|
| 144 | - * @param string $sortBy |
|
| 145 | - * @param boolean $desc |
|
| 146 | - * @param array $columns |
|
| 147 | - * @return collection |
|
| 148 | - */ |
|
| 149 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 150 | - { |
|
| 151 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 152 | - } |
|
| 138 | + /** |
|
| 139 | + * Fetch records from the storage based on the given |
|
| 140 | + * condition. |
|
| 141 | + * |
|
| 142 | + * @param array $conditions array of conditions |
|
| 143 | + * @param array $relations |
|
| 144 | + * @param string $sortBy |
|
| 145 | + * @param boolean $desc |
|
| 146 | + * @param array $columns |
|
| 147 | + * @return collection |
|
| 148 | + */ |
|
| 149 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 150 | + { |
|
| 151 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Fetch the first record from the storage based on the given |
|
| 156 | - * condition. |
|
| 157 | - * |
|
| 158 | - * @param array $conditions array of conditions |
|
| 159 | - * @param array $relations |
|
| 160 | - * @param array $columns |
|
| 161 | - * @return object |
|
| 162 | - */ |
|
| 163 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 164 | - { |
|
| 165 | - return $this->repo->first($conditions, $relations, $columns); |
|
| 166 | - } |
|
| 154 | + /** |
|
| 155 | + * Fetch the first record from the storage based on the given |
|
| 156 | + * condition. |
|
| 157 | + * |
|
| 158 | + * @param array $conditions array of conditions |
|
| 159 | + * @param array $relations |
|
| 160 | + * @param array $columns |
|
| 161 | + * @return object |
|
| 162 | + */ |
|
| 163 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 164 | + { |
|
| 165 | + return $this->repo->first($conditions, $relations, $columns); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Return the deleted models in pages based on the given conditions. |
|
| 170 | - * |
|
| 171 | - * @param array $conditions array of conditions |
|
| 172 | - * @param integer $perPage |
|
| 173 | - * @param string $sortBy |
|
| 174 | - * @param boolean $desc |
|
| 175 | - * @param array $columns |
|
| 176 | - * @return collection |
|
| 177 | - */ |
|
| 178 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 179 | - { |
|
| 180 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 181 | - } |
|
| 168 | + /** |
|
| 169 | + * Return the deleted models in pages based on the given conditions. |
|
| 170 | + * |
|
| 171 | + * @param array $conditions array of conditions |
|
| 172 | + * @param integer $perPage |
|
| 173 | + * @param string $sortBy |
|
| 174 | + * @param boolean $desc |
|
| 175 | + * @param array $columns |
|
| 176 | + * @return collection |
|
| 177 | + */ |
|
| 178 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 179 | + { |
|
| 180 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Restore the deleted model. |
|
| 185 | - * |
|
| 186 | - * @param integer $id |
|
| 187 | - * @return void |
|
| 188 | - */ |
|
| 189 | - public function restore($id) |
|
| 190 | - { |
|
| 191 | - return $this->repo->restore($id); |
|
| 192 | - } |
|
| 183 | + /** |
|
| 184 | + * Restore the deleted model. |
|
| 185 | + * |
|
| 186 | + * @param integer $id |
|
| 187 | + * @return void |
|
| 188 | + */ |
|
| 189 | + public function restore($id) |
|
| 190 | + { |
|
| 191 | + return $this->repo->restore($id); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Prepare filters for repo. |
|
| 196 | - * |
|
| 197 | - * @param array $conditions |
|
| 198 | - * @return array |
|
| 199 | - */ |
|
| 200 | - public function constructFilters($conditions) |
|
| 201 | - { |
|
| 202 | - $filters = []; |
|
| 203 | - $translatable = $this->repo->model->translatable ?? []; |
|
| 204 | - foreach ($conditions as $key => $value) { |
|
| 205 | - if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
| 206 | - /** |
|
| 207 | - * Prepare key based on the the requested lang if it was translatable. |
|
| 208 | - */ |
|
| 209 | - $key = in_array($key, $translatable) ? $key . '->' . (Session::get('locale') == 'all' ? 'en' : Session::get('locale')) : $key; |
|
| 194 | + /** |
|
| 195 | + * Prepare filters for repo. |
|
| 196 | + * |
|
| 197 | + * @param array $conditions |
|
| 198 | + * @return array |
|
| 199 | + */ |
|
| 200 | + public function constructFilters($conditions) |
|
| 201 | + { |
|
| 202 | + $filters = []; |
|
| 203 | + $translatable = $this->repo->model->translatable ?? []; |
|
| 204 | + foreach ($conditions as $key => $value) { |
|
| 205 | + if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
| 206 | + /** |
|
| 207 | + * Prepare key based on the the requested lang if it was translatable. |
|
| 208 | + */ |
|
| 209 | + $key = in_array($key, $translatable) ? $key . '->' . (Session::get('locale') == 'all' ? 'en' : Session::get('locale')) : $key; |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Convert 0/1 or true/false to boolean in case of not foreign key. |
|
| 213 | - */ |
|
| 214 | - if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
| 215 | - $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 211 | + /** |
|
| 212 | + * Convert 0/1 or true/false to boolean in case of not foreign key. |
|
| 213 | + */ |
|
| 214 | + if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
| 215 | + $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Use in operator in case of foreign and comma seperated values. |
|
| 219 | - */ |
|
| 220 | - } elseif (! is_array($value) && strpos($key, '_id') && $value) { |
|
| 221 | - $filters[$key] = [ |
|
| 222 | - 'op' => 'in', |
|
| 223 | - 'val' => explode(',', $value) |
|
| 224 | - ]; |
|
| 217 | + /** |
|
| 218 | + * Use in operator in case of foreign and comma seperated values. |
|
| 219 | + */ |
|
| 220 | + } elseif (! is_array($value) && strpos($key, '_id') && $value) { |
|
| 221 | + $filters[$key] = [ |
|
| 222 | + 'op' => 'in', |
|
| 223 | + 'val' => explode(',', $value) |
|
| 224 | + ]; |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Use null operator in case of 0 value and foreign. |
|
| 228 | - */ |
|
| 229 | - } elseif (strpos($key, '_id') && $value == 0) { |
|
| 230 | - $filters[$key] = [ |
|
| 231 | - 'op' => 'null' |
|
| 232 | - ]; |
|
| 226 | + /** |
|
| 227 | + * Use null operator in case of 0 value and foreign. |
|
| 228 | + */ |
|
| 229 | + } elseif (strpos($key, '_id') && $value == 0) { |
|
| 230 | + $filters[$key] = [ |
|
| 231 | + 'op' => 'null' |
|
| 232 | + ]; |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Consider values as a sub conditions if it is array. |
|
| 236 | - */ |
|
| 237 | - } elseif (is_array($value)) { |
|
| 238 | - $filters[$key] = $value; |
|
| 234 | + /** |
|
| 235 | + * Consider values as a sub conditions if it is array. |
|
| 236 | + */ |
|
| 237 | + } elseif (is_array($value)) { |
|
| 238 | + $filters[$key] = $value; |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Default string filteration. |
|
| 242 | - */ |
|
| 243 | - } elseif ($value) { |
|
| 244 | - $key = 'LOWER(' . $key . ')'; |
|
| 245 | - $value = strtolower($value); |
|
| 246 | - $filters[$key] = [ |
|
| 247 | - 'op' => 'like', |
|
| 248 | - 'val' => '%' . $value . '%' |
|
| 249 | - ]; |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 240 | + /** |
|
| 241 | + * Default string filteration. |
|
| 242 | + */ |
|
| 243 | + } elseif ($value) { |
|
| 244 | + $key = 'LOWER(' . $key . ')'; |
|
| 245 | + $value = strtolower($value); |
|
| 246 | + $filters[$key] = [ |
|
| 247 | + 'op' => 'like', |
|
| 248 | + 'val' => '%' . $value . '%' |
|
| 249 | + ]; |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - return $filters; |
|
| 255 | - } |
|
| 254 | + return $filters; |
|
| 255 | + } |
|
| 256 | 256 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | { |
| 39 | 39 | $translatable = $this->repo->model->translatable ?? []; |
| 40 | 40 | $filters = $this->constructFilters($conditions); |
| 41 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . Session::get('locale') : $sortBy; |
|
| 41 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy.'->'.Session::get('locale') : $sortBy; |
|
| 42 | 42 | |
| 43 | 43 | if ($trashed) { |
| 44 | 44 | return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | /** |
| 207 | 207 | * Prepare key based on the the requested lang if it was translatable. |
| 208 | 208 | */ |
| 209 | - $key = in_array($key, $translatable) ? $key . '->' . (Session::get('locale') == 'all' ? 'en' : Session::get('locale')) : $key; |
|
| 209 | + $key = in_array($key, $translatable) ? $key.'->'.(Session::get('locale') == 'all' ? 'en' : Session::get('locale')) : $key; |
|
| 210 | 210 | |
| 211 | 211 | /** |
| 212 | 212 | * Convert 0/1 or true/false to boolean in case of not foreign key. |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | /** |
| 218 | 218 | * Use in operator in case of foreign and comma seperated values. |
| 219 | 219 | */ |
| 220 | - } elseif (! is_array($value) && strpos($key, '_id') && $value) { |
|
| 220 | + } elseif ( ! is_array($value) && strpos($key, '_id') && $value) { |
|
| 221 | 221 | $filters[$key] = [ |
| 222 | 222 | 'op' => 'in', |
| 223 | 223 | 'val' => explode(',', $value) |
@@ -241,11 +241,11 @@ discard block |
||
| 241 | 241 | * Default string filteration. |
| 242 | 242 | */ |
| 243 | 243 | } elseif ($value) { |
| 244 | - $key = 'LOWER(' . $key . ')'; |
|
| 244 | + $key = 'LOWER('.$key.')'; |
|
| 245 | 245 | $value = strtolower($value); |
| 246 | 246 | $filters[$key] = [ |
| 247 | 247 | 'op' => 'like', |
| 248 | - 'val' => '%' . $value . '%' |
|
| 248 | + 'val' => '%'.$value.'%' |
|
| 249 | 249 | ]; |
| 250 | 250 | } |
| 251 | 251 | } |
@@ -8,612 +8,612 @@ |
||
| 8 | 8 | |
| 9 | 9 | abstract class BaseRepository implements BaseRepositoryInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var object |
|
| 13 | - */ |
|
| 14 | - public $model; |
|
| 11 | + /** |
|
| 12 | + * @var object |
|
| 13 | + */ |
|
| 14 | + public $model; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Init new object. |
|
| 18 | - * |
|
| 19 | - * @var mixed model |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function __construct($model) |
|
| 23 | - { |
|
| 24 | - $this->model = $model; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Fetch all records with relations from the storage. |
|
| 29 | - * |
|
| 30 | - * @param array $relations |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @param array $columns |
|
| 34 | - * @return collection |
|
| 35 | - */ |
|
| 36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | - { |
|
| 38 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | - } |
|
| 16 | + /** |
|
| 17 | + * Init new object. |
|
| 18 | + * |
|
| 19 | + * @var mixed model |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function __construct($model) |
|
| 23 | + { |
|
| 24 | + $this->model = $model; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Fetch all records with relations from the storage. |
|
| 29 | + * |
|
| 30 | + * @param array $relations |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @param array $columns |
|
| 34 | + * @return collection |
|
| 35 | + */ |
|
| 36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | + { |
|
| 38 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Fetch all records with relations from storage in pages. |
|
| 44 | - * |
|
| 45 | - * @param integer $perPage |
|
| 46 | - * @param array $relations |
|
| 47 | - * @param string $sortBy |
|
| 48 | - * @param boolean $desc |
|
| 49 | - * @param array $columns |
|
| 50 | - * @return collection |
|
| 51 | - */ |
|
| 52 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 53 | - { |
|
| 54 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 55 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Fetch all records with relations based on |
|
| 60 | - * the given condition from storage in pages. |
|
| 61 | - * |
|
| 62 | - * @param array $conditions array of conditions |
|
| 63 | - * @param integer $perPage |
|
| 64 | - * @param array $relations |
|
| 65 | - * @param string $sortBy |
|
| 66 | - * @param boolean $desc |
|
| 67 | - * @param array $columns |
|
| 68 | - * @return collection |
|
| 69 | - */ |
|
| 70 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 71 | - { |
|
| 72 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 73 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 74 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Count all records based on the given condition from storage. |
|
| 79 | - * |
|
| 80 | - * @param array $conditions array of conditions |
|
| 81 | - * @return collection |
|
| 82 | - */ |
|
| 83 | - public function count($conditions = false) |
|
| 84 | - { |
|
| 85 | - if($conditions) { |
|
| 86 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 87 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 88 | - } |
|
| 42 | + /** |
|
| 43 | + * Fetch all records with relations from storage in pages. |
|
| 44 | + * |
|
| 45 | + * @param integer $perPage |
|
| 46 | + * @param array $relations |
|
| 47 | + * @param string $sortBy |
|
| 48 | + * @param boolean $desc |
|
| 49 | + * @param array $columns |
|
| 50 | + * @return collection |
|
| 51 | + */ |
|
| 52 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 53 | + { |
|
| 54 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 55 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Fetch all records with relations based on |
|
| 60 | + * the given condition from storage in pages. |
|
| 61 | + * |
|
| 62 | + * @param array $conditions array of conditions |
|
| 63 | + * @param integer $perPage |
|
| 64 | + * @param array $relations |
|
| 65 | + * @param string $sortBy |
|
| 66 | + * @param boolean $desc |
|
| 67 | + * @param array $columns |
|
| 68 | + * @return collection |
|
| 69 | + */ |
|
| 70 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 71 | + { |
|
| 72 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 73 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 74 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Count all records based on the given condition from storage. |
|
| 79 | + * |
|
| 80 | + * @param array $conditions array of conditions |
|
| 81 | + * @return collection |
|
| 82 | + */ |
|
| 83 | + public function count($conditions = false) |
|
| 84 | + { |
|
| 85 | + if($conditions) { |
|
| 86 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 87 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $this->model->count(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Pluck column based on the given condition from storage. |
|
| 95 | - * |
|
| 96 | - * @param array $conditions array of conditions |
|
| 97 | - * @param string $column |
|
| 98 | - * @return collection |
|
| 99 | - */ |
|
| 100 | - public function pluck($conditions, $column) |
|
| 101 | - { |
|
| 102 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 103 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
| 104 | - } |
|
| 90 | + return $this->model->count(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Pluck column based on the given condition from storage. |
|
| 95 | + * |
|
| 96 | + * @param array $conditions array of conditions |
|
| 97 | + * @param string $column |
|
| 98 | + * @return collection |
|
| 99 | + */ |
|
| 100 | + public function pluck($conditions, $column) |
|
| 101 | + { |
|
| 102 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 103 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Save the given model to the storage. |
|
| 108 | - * |
|
| 109 | - * @param array $data |
|
| 110 | - * @return mixed |
|
| 111 | - */ |
|
| 112 | - public function save(array $data) |
|
| 113 | - { |
|
| 114 | - $local = \Session::get('locale'); |
|
| 115 | - \Session::put('locale', 'all'); |
|
| 116 | - $model = false; |
|
| 117 | - $relations = []; |
|
| 118 | - |
|
| 119 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 106 | + /** |
|
| 107 | + * Save the given model to the storage. |
|
| 108 | + * |
|
| 109 | + * @param array $data |
|
| 110 | + * @return mixed |
|
| 111 | + */ |
|
| 112 | + public function save(array $data) |
|
| 113 | + { |
|
| 114 | + $local = \Session::get('locale'); |
|
| 115 | + \Session::put('locale', 'all'); |
|
| 116 | + $model = false; |
|
| 117 | + $relations = []; |
|
| 118 | + |
|
| 119 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 120 | 120 | |
| 121 | - $model = $this->prepareModel($data); |
|
| 122 | - $relations = $this->prepareRelations($data, $model); |
|
| 123 | - $model = $this->saveModel($model, $relations); |
|
| 124 | - }); |
|
| 125 | - \Session::put('locale', $local); |
|
| 121 | + $model = $this->prepareModel($data); |
|
| 122 | + $relations = $this->prepareRelations($data, $model); |
|
| 123 | + $model = $this->saveModel($model, $relations); |
|
| 124 | + }); |
|
| 125 | + \Session::put('locale', $local); |
|
| 126 | 126 | |
| 127 | - if (count($relations)) { |
|
| 128 | - $model->load(...array_keys($relations)); |
|
| 129 | - } |
|
| 127 | + if (count($relations)) { |
|
| 128 | + $model->load(...array_keys($relations)); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - return $model; |
|
| 132 | - } |
|
| 131 | + return $model; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Insert the given model/models to the storage. |
|
| 136 | - * |
|
| 137 | - * @param array $data |
|
| 138 | - * @return mixed |
|
| 139 | - */ |
|
| 140 | - public function insert(array $data) |
|
| 141 | - { |
|
| 142 | - return $this->model->insert($data); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Delete record from the storage based on the given |
|
| 147 | - * condition. |
|
| 148 | - * |
|
| 149 | - * @param var $value condition value |
|
| 150 | - * @param string $attribute condition column name |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - public function delete($value, $attribute = 'id') |
|
| 154 | - { |
|
| 155 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 156 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 157 | - $model->delete(); |
|
| 158 | - }); |
|
| 159 | - }); |
|
| 160 | - } |
|
| 134 | + /** |
|
| 135 | + * Insert the given model/models to the storage. |
|
| 136 | + * |
|
| 137 | + * @param array $data |
|
| 138 | + * @return mixed |
|
| 139 | + */ |
|
| 140 | + public function insert(array $data) |
|
| 141 | + { |
|
| 142 | + return $this->model->insert($data); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Delete record from the storage based on the given |
|
| 147 | + * condition. |
|
| 148 | + * |
|
| 149 | + * @param var $value condition value |
|
| 150 | + * @param string $attribute condition column name |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + public function delete($value, $attribute = 'id') |
|
| 154 | + { |
|
| 155 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 156 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 157 | + $model->delete(); |
|
| 158 | + }); |
|
| 159 | + }); |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Fetch records from the storage based on the given |
|
| 164 | - * id. |
|
| 165 | - * |
|
| 166 | - * @param integer $id |
|
| 167 | - * @param string[] $relations |
|
| 168 | - * @param array $columns |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 172 | - { |
|
| 173 | - return $this->model->with($relations)->find($id, $columns); |
|
| 174 | - } |
|
| 162 | + /** |
|
| 163 | + * Fetch records from the storage based on the given |
|
| 164 | + * id. |
|
| 165 | + * |
|
| 166 | + * @param integer $id |
|
| 167 | + * @param string[] $relations |
|
| 168 | + * @param array $columns |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 172 | + { |
|
| 173 | + return $this->model->with($relations)->find($id, $columns); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Fetch records from the storage based on the given |
|
| 178 | - * condition. |
|
| 179 | - * |
|
| 180 | - * @param array $conditions array of conditions |
|
| 181 | - * @param array $relations |
|
| 182 | - * @param string $sortBy |
|
| 183 | - * @param boolean $desc |
|
| 184 | - * @param array $columns |
|
| 185 | - * @return collection |
|
| 186 | - */ |
|
| 187 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 188 | - { |
|
| 189 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 190 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 191 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Fetch the first record from the storage based on the given |
|
| 196 | - * condition. |
|
| 197 | - * |
|
| 198 | - * @param array $conditions array of conditions |
|
| 199 | - * @param array $relations |
|
| 200 | - * @param array $columns |
|
| 201 | - * @return object |
|
| 202 | - */ |
|
| 203 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 204 | - { |
|
| 205 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 206 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Return the deleted models in pages based on the given conditions. |
|
| 211 | - * |
|
| 212 | - * @param array $conditions array of conditions |
|
| 213 | - * @param integer $perPage |
|
| 214 | - * @param string $sortBy |
|
| 215 | - * @param boolean $desc |
|
| 216 | - * @param array $columns |
|
| 217 | - * @return collection |
|
| 218 | - */ |
|
| 219 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 220 | - { |
|
| 221 | - unset($conditions['page']); |
|
| 222 | - unset($conditions['perPage']); |
|
| 223 | - unset($conditions['sortBy']); |
|
| 224 | - unset($conditions['sort']); |
|
| 225 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 226 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 227 | - $model = $this->model->onlyTrashed(); |
|
| 228 | - |
|
| 229 | - if (count($conditions['conditionValues'])) { |
|
| 230 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Restore the deleted model. |
|
| 238 | - * |
|
| 239 | - * @param integer $id |
|
| 240 | - * @return void |
|
| 241 | - */ |
|
| 242 | - public function restore($id) |
|
| 243 | - { |
|
| 244 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 245 | - |
|
| 246 | - if (! $model) { |
|
| 247 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $model->restore(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Fill the model with the given data. |
|
| 255 | - * |
|
| 256 | - * @param array $data |
|
| 257 | - * |
|
| 258 | - * @return object |
|
| 259 | - */ |
|
| 260 | - public function prepareModel($data) |
|
| 261 | - { |
|
| 262 | - $modelClass = $this->model; |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * If the id is present in the data then select the model for updating, |
|
| 266 | - * else create new model. |
|
| 267 | - * @var array |
|
| 268 | - */ |
|
| 269 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 270 | - if (! $model) { |
|
| 271 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Construct the model object with the given data, |
|
| 276 | - * and if there is a relation add it to relations array, |
|
| 277 | - * then save the model. |
|
| 278 | - */ |
|
| 279 | - foreach ($data as $key => $value) { |
|
| 280 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 281 | - /** |
|
| 282 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 283 | - */ |
|
| 284 | - $model->$key = $value; |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - return $model; |
|
| 289 | - } |
|
| 176 | + /** |
|
| 177 | + * Fetch records from the storage based on the given |
|
| 178 | + * condition. |
|
| 179 | + * |
|
| 180 | + * @param array $conditions array of conditions |
|
| 181 | + * @param array $relations |
|
| 182 | + * @param string $sortBy |
|
| 183 | + * @param boolean $desc |
|
| 184 | + * @param array $columns |
|
| 185 | + * @return collection |
|
| 186 | + */ |
|
| 187 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 188 | + { |
|
| 189 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 190 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 191 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Fetch the first record from the storage based on the given |
|
| 196 | + * condition. |
|
| 197 | + * |
|
| 198 | + * @param array $conditions array of conditions |
|
| 199 | + * @param array $relations |
|
| 200 | + * @param array $columns |
|
| 201 | + * @return object |
|
| 202 | + */ |
|
| 203 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 204 | + { |
|
| 205 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 206 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Return the deleted models in pages based on the given conditions. |
|
| 211 | + * |
|
| 212 | + * @param array $conditions array of conditions |
|
| 213 | + * @param integer $perPage |
|
| 214 | + * @param string $sortBy |
|
| 215 | + * @param boolean $desc |
|
| 216 | + * @param array $columns |
|
| 217 | + * @return collection |
|
| 218 | + */ |
|
| 219 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 220 | + { |
|
| 221 | + unset($conditions['page']); |
|
| 222 | + unset($conditions['perPage']); |
|
| 223 | + unset($conditions['sortBy']); |
|
| 224 | + unset($conditions['sort']); |
|
| 225 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 226 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 227 | + $model = $this->model->onlyTrashed(); |
|
| 228 | + |
|
| 229 | + if (count($conditions['conditionValues'])) { |
|
| 230 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Restore the deleted model. |
|
| 238 | + * |
|
| 239 | + * @param integer $id |
|
| 240 | + * @return void |
|
| 241 | + */ |
|
| 242 | + public function restore($id) |
|
| 243 | + { |
|
| 244 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 245 | + |
|
| 246 | + if (! $model) { |
|
| 247 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $model->restore(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Fill the model with the given data. |
|
| 255 | + * |
|
| 256 | + * @param array $data |
|
| 257 | + * |
|
| 258 | + * @return object |
|
| 259 | + */ |
|
| 260 | + public function prepareModel($data) |
|
| 261 | + { |
|
| 262 | + $modelClass = $this->model; |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * If the id is present in the data then select the model for updating, |
|
| 266 | + * else create new model. |
|
| 267 | + * @var array |
|
| 268 | + */ |
|
| 269 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 270 | + if (! $model) { |
|
| 271 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Construct the model object with the given data, |
|
| 276 | + * and if there is a relation add it to relations array, |
|
| 277 | + * then save the model. |
|
| 278 | + */ |
|
| 279 | + foreach ($data as $key => $value) { |
|
| 280 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 281 | + /** |
|
| 282 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 283 | + */ |
|
| 284 | + $model->$key = $value; |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + return $model; |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Prepare related models based on the given data for the given model. |
|
| 293 | - * |
|
| 294 | - * @param array $data |
|
| 295 | - * @param object $model |
|
| 296 | - * |
|
| 297 | - * @return array |
|
| 298 | - */ |
|
| 299 | - public function prepareRelations($data, $model) |
|
| 300 | - { |
|
| 301 | - /** |
|
| 302 | - * Init the relation array |
|
| 303 | - * |
|
| 304 | - * @var array |
|
| 305 | - */ |
|
| 306 | - $relations = []; |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Construct the model object with the given data, |
|
| 310 | - * and if there is a relation add it to relations array, |
|
| 311 | - * then save the model. |
|
| 312 | - */ |
|
| 313 | - foreach ($data as $key => $value) { |
|
| 314 | - /** |
|
| 315 | - * If the attribute is a relation. |
|
| 316 | - */ |
|
| 317 | - $relation = \Str::camel($key); |
|
| 318 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 319 | - /** |
|
| 320 | - * Check if the relation is a collection. |
|
| 321 | - */ |
|
| 322 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 323 | - /** |
|
| 324 | - * If the relation has no value then marke the relation data |
|
| 325 | - * related to the model to be deleted. |
|
| 326 | - */ |
|
| 327 | - if (! $value || ! count($value)) { |
|
| 328 | - $relations[$relation] = 'delete'; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - if (is_array($value)) { |
|
| 332 | - /** |
|
| 333 | - * Loop through the relation data. |
|
| 334 | - */ |
|
| 335 | - foreach ($value as $attr => $val) { |
|
| 336 | - /** |
|
| 337 | - * Get the relation model. |
|
| 338 | - */ |
|
| 339 | - $relationBaseModel = \Core::$relation()->model; |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Check if the relation is a collection. |
|
| 343 | - */ |
|
| 344 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 345 | - if (! is_array($val)) { |
|
| 346 | - $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
| 347 | - } else { |
|
| 348 | - /** |
|
| 349 | - * If the id is present in the data then select the relation model for updating, |
|
| 350 | - * else create new model. |
|
| 351 | - */ |
|
| 352 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * If model doesn't exists. |
|
| 357 | - */ |
|
| 358 | - if (! $relationModel) { |
|
| 359 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if (is_array($val)) { |
|
| 363 | - /** |
|
| 364 | - * Loop through the relation attributes. |
|
| 365 | - */ |
|
| 366 | - foreach ($val as $attr => $val) { |
|
| 367 | - /** |
|
| 368 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 369 | - */ |
|
| 370 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false && class_basename($model->$key()) !== 'BelongsToMany') { |
|
| 371 | - $relationModel->$attr = $val; |
|
| 372 | - } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 373 | - $extra[$attr] = $val; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (isset($extra)) { |
|
| 379 | - $relationModel->extra = $extra; |
|
| 380 | - } |
|
| 381 | - $relations[$relation][] = $relationModel; |
|
| 382 | - } else { |
|
| 383 | - /** |
|
| 384 | - * Prevent the sub relations. |
|
| 385 | - */ |
|
| 386 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 387 | - /** |
|
| 388 | - * If the id is present in the data then select the relation model for updating, |
|
| 389 | - * else create new model. |
|
| 390 | - */ |
|
| 391 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * If model doesn't exists. |
|
| 395 | - */ |
|
| 396 | - if (! $relationModel) { |
|
| 397 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 401 | - /** |
|
| 402 | - * Prevent attributes not in the fillable. |
|
| 403 | - */ |
|
| 404 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 405 | - $relationModel->$relationAttribute = $relationValue; |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $relations[$relation] = $relationModel; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - return $relations; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Save the model with related models. |
|
| 422 | - * |
|
| 423 | - * @param object $model |
|
| 424 | - * @param array $relations |
|
| 425 | - * |
|
| 426 | - * @return object |
|
| 427 | - */ |
|
| 428 | - public function saveModel($model, $relations) |
|
| 429 | - { |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Loop through the relations array. |
|
| 433 | - */ |
|
| 434 | - foreach ($relations as $key => $value) { |
|
| 435 | - /** |
|
| 436 | - * If the relation is marked for delete then delete it. |
|
| 437 | - */ |
|
| 438 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 439 | - $model->$key()->delete(); |
|
| 440 | - } elseif (gettype($value) == 'array') { |
|
| 441 | - /** |
|
| 442 | - * Save the model. |
|
| 443 | - */ |
|
| 444 | - $model->save(); |
|
| 445 | - $ids = []; |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Loop through the relations. |
|
| 449 | - */ |
|
| 450 | - foreach ($value as $val) { |
|
| 451 | - switch (class_basename($model->$key())) { |
|
| 452 | - /** |
|
| 453 | - * If the relation is one to many then update it's foreign key with |
|
| 454 | - * the model id and save it then add its id to ids array to delete all |
|
| 455 | - * relations who's id isn't in the ids array. |
|
| 456 | - */ |
|
| 457 | - case 'HasMany': |
|
| 458 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 459 | - $val->$foreignKeyName = $model->id; |
|
| 460 | - $val->save(); |
|
| 461 | - $ids[] = $val->id; |
|
| 462 | - break; |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * If the relation is many to many then add it's id to the ids array to |
|
| 466 | - * attache these ids to the model. |
|
| 467 | - */ |
|
| 468 | - case 'BelongsToMany': |
|
| 469 | - case 'MorphToMany': |
|
| 470 | - $extra = $val->extra; |
|
| 471 | - unset($val->extra); |
|
| 472 | - $val->save(); |
|
| 473 | - $ids[$val->id] = $extra ?? []; |
|
| 474 | - break; |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - switch (class_basename($model->$key())) { |
|
| 478 | - /** |
|
| 479 | - * If the relation is one to many then delete all |
|
| 480 | - * relations who's id isn't in the ids array. |
|
| 481 | - */ |
|
| 482 | - case 'HasMany': |
|
| 483 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 484 | - break; |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * If the relation is many to many then |
|
| 488 | - * detach the previous data and attach |
|
| 489 | - * the ids array to the model. |
|
| 490 | - */ |
|
| 491 | - case 'BelongsToMany': |
|
| 492 | - case 'MorphToMany': |
|
| 493 | - $model->$key()->detach(); |
|
| 494 | - $model->$key()->attach($ids); |
|
| 495 | - break; |
|
| 496 | - } |
|
| 497 | - } else { |
|
| 498 | - switch (class_basename($model->$key())) { |
|
| 499 | - /** |
|
| 500 | - * If the relation is one to one. |
|
| 501 | - */ |
|
| 502 | - case 'HasOne': |
|
| 503 | - /** |
|
| 504 | - * Save the model. |
|
| 505 | - */ |
|
| 506 | - $model->save(); |
|
| 507 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 508 | - $value->$foreignKeyName = $model->id; |
|
| 509 | - $value->save(); |
|
| 510 | - break; |
|
| 511 | - case 'BelongsTo': |
|
| 512 | - /** |
|
| 513 | - * Save the model. |
|
| 514 | - */ |
|
| 515 | - $value->save(); |
|
| 516 | - $model->$key()->associate($value); |
|
| 517 | - break; |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Save the model. |
|
| 524 | - */ |
|
| 525 | - $model->save(); |
|
| 526 | - |
|
| 527 | - return $model; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Build the conditions recursively for the retrieving methods. |
|
| 532 | - * @param array $conditions |
|
| 533 | - * @return array |
|
| 534 | - */ |
|
| 535 | - protected function constructConditions($conditions, $model) |
|
| 536 | - { |
|
| 537 | - $conditionString = ''; |
|
| 538 | - $conditionValues = []; |
|
| 539 | - foreach ($conditions as $key => $value) { |
|
| 540 | - if (Str::contains($key, '->')) { |
|
| 541 | - $key = $this->wrapJsonSelector($key); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - if ($key == 'and') { |
|
| 545 | - $conditions = $this->constructConditions($value, $model); |
|
| 546 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 547 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 548 | - } elseif ($key == 'or') { |
|
| 549 | - $conditions = $this->constructConditions($value, $model); |
|
| 550 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 551 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 552 | - } else { |
|
| 553 | - if (is_array($value)) { |
|
| 554 | - $operator = $value['op']; |
|
| 555 | - if (strtolower($operator) == 'between') { |
|
| 556 | - $value1 = $value['val1']; |
|
| 557 | - $value2 = $value['val2']; |
|
| 558 | - } else { |
|
| 559 | - $value = Arr::get($value, 'val', ''); |
|
| 560 | - } |
|
| 561 | - } else { |
|
| 562 | - $operator = '='; |
|
| 563 | - } |
|
| 291 | + /** |
|
| 292 | + * Prepare related models based on the given data for the given model. |
|
| 293 | + * |
|
| 294 | + * @param array $data |
|
| 295 | + * @param object $model |
|
| 296 | + * |
|
| 297 | + * @return array |
|
| 298 | + */ |
|
| 299 | + public function prepareRelations($data, $model) |
|
| 300 | + { |
|
| 301 | + /** |
|
| 302 | + * Init the relation array |
|
| 303 | + * |
|
| 304 | + * @var array |
|
| 305 | + */ |
|
| 306 | + $relations = []; |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Construct the model object with the given data, |
|
| 310 | + * and if there is a relation add it to relations array, |
|
| 311 | + * then save the model. |
|
| 312 | + */ |
|
| 313 | + foreach ($data as $key => $value) { |
|
| 314 | + /** |
|
| 315 | + * If the attribute is a relation. |
|
| 316 | + */ |
|
| 317 | + $relation = \Str::camel($key); |
|
| 318 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 319 | + /** |
|
| 320 | + * Check if the relation is a collection. |
|
| 321 | + */ |
|
| 322 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 323 | + /** |
|
| 324 | + * If the relation has no value then marke the relation data |
|
| 325 | + * related to the model to be deleted. |
|
| 326 | + */ |
|
| 327 | + if (! $value || ! count($value)) { |
|
| 328 | + $relations[$relation] = 'delete'; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + if (is_array($value)) { |
|
| 332 | + /** |
|
| 333 | + * Loop through the relation data. |
|
| 334 | + */ |
|
| 335 | + foreach ($value as $attr => $val) { |
|
| 336 | + /** |
|
| 337 | + * Get the relation model. |
|
| 338 | + */ |
|
| 339 | + $relationBaseModel = \Core::$relation()->model; |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Check if the relation is a collection. |
|
| 343 | + */ |
|
| 344 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 345 | + if (! is_array($val)) { |
|
| 346 | + $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
| 347 | + } else { |
|
| 348 | + /** |
|
| 349 | + * If the id is present in the data then select the relation model for updating, |
|
| 350 | + * else create new model. |
|
| 351 | + */ |
|
| 352 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * If model doesn't exists. |
|
| 357 | + */ |
|
| 358 | + if (! $relationModel) { |
|
| 359 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if (is_array($val)) { |
|
| 363 | + /** |
|
| 364 | + * Loop through the relation attributes. |
|
| 365 | + */ |
|
| 366 | + foreach ($val as $attr => $val) { |
|
| 367 | + /** |
|
| 368 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 369 | + */ |
|
| 370 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false && class_basename($model->$key()) !== 'BelongsToMany') { |
|
| 371 | + $relationModel->$attr = $val; |
|
| 372 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 373 | + $extra[$attr] = $val; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (isset($extra)) { |
|
| 379 | + $relationModel->extra = $extra; |
|
| 380 | + } |
|
| 381 | + $relations[$relation][] = $relationModel; |
|
| 382 | + } else { |
|
| 383 | + /** |
|
| 384 | + * Prevent the sub relations. |
|
| 385 | + */ |
|
| 386 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 387 | + /** |
|
| 388 | + * If the id is present in the data then select the relation model for updating, |
|
| 389 | + * else create new model. |
|
| 390 | + */ |
|
| 391 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * If model doesn't exists. |
|
| 395 | + */ |
|
| 396 | + if (! $relationModel) { |
|
| 397 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 401 | + /** |
|
| 402 | + * Prevent attributes not in the fillable. |
|
| 403 | + */ |
|
| 404 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 405 | + $relationModel->$relationAttribute = $relationValue; |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $relations[$relation] = $relationModel; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + return $relations; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Save the model with related models. |
|
| 422 | + * |
|
| 423 | + * @param object $model |
|
| 424 | + * @param array $relations |
|
| 425 | + * |
|
| 426 | + * @return object |
|
| 427 | + */ |
|
| 428 | + public function saveModel($model, $relations) |
|
| 429 | + { |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Loop through the relations array. |
|
| 433 | + */ |
|
| 434 | + foreach ($relations as $key => $value) { |
|
| 435 | + /** |
|
| 436 | + * If the relation is marked for delete then delete it. |
|
| 437 | + */ |
|
| 438 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 439 | + $model->$key()->delete(); |
|
| 440 | + } elseif (gettype($value) == 'array') { |
|
| 441 | + /** |
|
| 442 | + * Save the model. |
|
| 443 | + */ |
|
| 444 | + $model->save(); |
|
| 445 | + $ids = []; |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Loop through the relations. |
|
| 449 | + */ |
|
| 450 | + foreach ($value as $val) { |
|
| 451 | + switch (class_basename($model->$key())) { |
|
| 452 | + /** |
|
| 453 | + * If the relation is one to many then update it's foreign key with |
|
| 454 | + * the model id and save it then add its id to ids array to delete all |
|
| 455 | + * relations who's id isn't in the ids array. |
|
| 456 | + */ |
|
| 457 | + case 'HasMany': |
|
| 458 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 459 | + $val->$foreignKeyName = $model->id; |
|
| 460 | + $val->save(); |
|
| 461 | + $ids[] = $val->id; |
|
| 462 | + break; |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * If the relation is many to many then add it's id to the ids array to |
|
| 466 | + * attache these ids to the model. |
|
| 467 | + */ |
|
| 468 | + case 'BelongsToMany': |
|
| 469 | + case 'MorphToMany': |
|
| 470 | + $extra = $val->extra; |
|
| 471 | + unset($val->extra); |
|
| 472 | + $val->save(); |
|
| 473 | + $ids[$val->id] = $extra ?? []; |
|
| 474 | + break; |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + switch (class_basename($model->$key())) { |
|
| 478 | + /** |
|
| 479 | + * If the relation is one to many then delete all |
|
| 480 | + * relations who's id isn't in the ids array. |
|
| 481 | + */ |
|
| 482 | + case 'HasMany': |
|
| 483 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 484 | + break; |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * If the relation is many to many then |
|
| 488 | + * detach the previous data and attach |
|
| 489 | + * the ids array to the model. |
|
| 490 | + */ |
|
| 491 | + case 'BelongsToMany': |
|
| 492 | + case 'MorphToMany': |
|
| 493 | + $model->$key()->detach(); |
|
| 494 | + $model->$key()->attach($ids); |
|
| 495 | + break; |
|
| 496 | + } |
|
| 497 | + } else { |
|
| 498 | + switch (class_basename($model->$key())) { |
|
| 499 | + /** |
|
| 500 | + * If the relation is one to one. |
|
| 501 | + */ |
|
| 502 | + case 'HasOne': |
|
| 503 | + /** |
|
| 504 | + * Save the model. |
|
| 505 | + */ |
|
| 506 | + $model->save(); |
|
| 507 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 508 | + $value->$foreignKeyName = $model->id; |
|
| 509 | + $value->save(); |
|
| 510 | + break; |
|
| 511 | + case 'BelongsTo': |
|
| 512 | + /** |
|
| 513 | + * Save the model. |
|
| 514 | + */ |
|
| 515 | + $value->save(); |
|
| 516 | + $model->$key()->associate($value); |
|
| 517 | + break; |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Save the model. |
|
| 524 | + */ |
|
| 525 | + $model->save(); |
|
| 526 | + |
|
| 527 | + return $model; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Build the conditions recursively for the retrieving methods. |
|
| 532 | + * @param array $conditions |
|
| 533 | + * @return array |
|
| 534 | + */ |
|
| 535 | + protected function constructConditions($conditions, $model) |
|
| 536 | + { |
|
| 537 | + $conditionString = ''; |
|
| 538 | + $conditionValues = []; |
|
| 539 | + foreach ($conditions as $key => $value) { |
|
| 540 | + if (Str::contains($key, '->')) { |
|
| 541 | + $key = $this->wrapJsonSelector($key); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + if ($key == 'and') { |
|
| 545 | + $conditions = $this->constructConditions($value, $model); |
|
| 546 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 547 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 548 | + } elseif ($key == 'or') { |
|
| 549 | + $conditions = $this->constructConditions($value, $model); |
|
| 550 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 551 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 552 | + } else { |
|
| 553 | + if (is_array($value)) { |
|
| 554 | + $operator = $value['op']; |
|
| 555 | + if (strtolower($operator) == 'between') { |
|
| 556 | + $value1 = $value['val1']; |
|
| 557 | + $value2 = $value['val2']; |
|
| 558 | + } else { |
|
| 559 | + $value = Arr::get($value, 'val', ''); |
|
| 560 | + } |
|
| 561 | + } else { |
|
| 562 | + $operator = '='; |
|
| 563 | + } |
|
| 564 | 564 | |
| 565 | - if (strtolower($operator) == 'between') { |
|
| 566 | - $conditionString .= $key.' >= ? and '; |
|
| 567 | - $conditionValues[] = $value1; |
|
| 568 | - |
|
| 569 | - $conditionString .= $key.' <= ? {op} '; |
|
| 570 | - $conditionValues[] = $value2; |
|
| 571 | - } elseif (strtolower($operator) == 'in') { |
|
| 572 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 573 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 574 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 575 | - } elseif (strtolower($operator) == 'null') { |
|
| 576 | - $conditionString .= $key.' is null {op} '; |
|
| 577 | - } elseif (strtolower($operator) == 'not null') { |
|
| 578 | - $conditionString .= $key.' is not null {op} '; |
|
| 579 | - } elseif (strtolower($operator) == 'has') { |
|
| 580 | - $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
| 581 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
| 582 | - if($value) { |
|
| 583 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 584 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
| 585 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
| 586 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 587 | - } else { |
|
| 588 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).') {op} '; |
|
| 589 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
| 590 | - } |
|
| 591 | - } else { |
|
| 592 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 593 | - $conditionValues[] = $value; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 598 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Wrap the given JSON selector. |
|
| 603 | - * |
|
| 604 | - * @param string $value |
|
| 605 | - * @return string |
|
| 606 | - */ |
|
| 607 | - protected function wrapJsonSelector($value) |
|
| 608 | - { |
|
| 609 | - $removeLast = strpos($value, ')'); |
|
| 610 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 611 | - $path = explode('->', $value); |
|
| 612 | - $field = array_shift($path); |
|
| 613 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 614 | - return '"'.$part.'"'; |
|
| 615 | - })->implode('.')); |
|
| 565 | + if (strtolower($operator) == 'between') { |
|
| 566 | + $conditionString .= $key.' >= ? and '; |
|
| 567 | + $conditionValues[] = $value1; |
|
| 568 | + |
|
| 569 | + $conditionString .= $key.' <= ? {op} '; |
|
| 570 | + $conditionValues[] = $value2; |
|
| 571 | + } elseif (strtolower($operator) == 'in') { |
|
| 572 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 573 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 574 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 575 | + } elseif (strtolower($operator) == 'null') { |
|
| 576 | + $conditionString .= $key.' is null {op} '; |
|
| 577 | + } elseif (strtolower($operator) == 'not null') { |
|
| 578 | + $conditionString .= $key.' is not null {op} '; |
|
| 579 | + } elseif (strtolower($operator) == 'has') { |
|
| 580 | + $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
| 581 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
| 582 | + if($value) { |
|
| 583 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 584 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
| 585 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
| 586 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 587 | + } else { |
|
| 588 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).') {op} '; |
|
| 589 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
| 590 | + } |
|
| 591 | + } else { |
|
| 592 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 593 | + $conditionValues[] = $value; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 598 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Wrap the given JSON selector. |
|
| 603 | + * |
|
| 604 | + * @param string $value |
|
| 605 | + * @return string |
|
| 606 | + */ |
|
| 607 | + protected function wrapJsonSelector($value) |
|
| 608 | + { |
|
| 609 | + $removeLast = strpos($value, ')'); |
|
| 610 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 611 | + $path = explode('->', $value); |
|
| 612 | + $field = array_shift($path); |
|
| 613 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 614 | + return '"'.$part.'"'; |
|
| 615 | + })->implode('.')); |
|
| 616 | 616 | |
| 617 | - return $removeLast === false ? $result : $result.')'; |
|
| 618 | - } |
|
| 617 | + return $removeLast === false ? $result : $result.')'; |
|
| 618 | + } |
|
| 619 | 619 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __construct($model) |
| 23 | 23 | { |
| 24 | - $this->model = $model; |
|
| 24 | + $this->model = $model; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function count($conditions = false) |
| 84 | 84 | { |
| 85 | - if($conditions) { |
|
| 85 | + if ($conditions) { |
|
| 86 | 86 | $conditions = $this->constructConditions($conditions, $this->model); |
| 87 | 87 | return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
| 88 | 88 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | $model = false; |
| 117 | 117 | $relations = []; |
| 118 | 118 | |
| 119 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 119 | + \DB::transaction(function() use (&$model, &$relations, $data) { |
|
| 120 | 120 | |
| 121 | 121 | $model = $this->prepareModel($data); |
| 122 | 122 | $relations = $this->prepareRelations($data, $model); |
@@ -152,8 +152,8 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function delete($value, $attribute = 'id') |
| 154 | 154 | { |
| 155 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 156 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 155 | + \DB::transaction(function() use ($value, $attribute) { |
|
| 156 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
| 157 | 157 | $model->delete(); |
| 158 | 158 | }); |
| 159 | 159 | }); |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | { |
| 244 | 244 | $model = $this->model->onlyTrashed()->find($id); |
| 245 | 245 | |
| 246 | - if (! $model) { |
|
| 246 | + if ( ! $model) { |
|
| 247 | 247 | \Errors::notFound(class_basename($this->model).' with id : '.$id); |
| 248 | 248 | } |
| 249 | 249 | |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | * @var array |
| 268 | 268 | */ |
| 269 | 269 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 270 | - if (! $model) { |
|
| 270 | + if ( ! $model) { |
|
| 271 | 271 | \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
| 272 | 272 | } |
| 273 | 273 | |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | * If the relation has no value then marke the relation data |
| 325 | 325 | * related to the model to be deleted. |
| 326 | 326 | */ |
| 327 | - if (! $value || ! count($value)) { |
|
| 327 | + if ( ! $value || ! count($value)) { |
|
| 328 | 328 | $relations[$relation] = 'delete'; |
| 329 | 329 | } |
| 330 | 330 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | * Check if the relation is a collection. |
| 343 | 343 | */ |
| 344 | 344 | if (class_basename($model->$relation) == 'Collection') { |
| 345 | - if (! is_array($val)) { |
|
| 345 | + if ( ! is_array($val)) { |
|
| 346 | 346 | $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
| 347 | 347 | } else { |
| 348 | 348 | /** |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | /** |
| 356 | 356 | * If model doesn't exists. |
| 357 | 357 | */ |
| 358 | - if (! $relationModel) { |
|
| 358 | + if ( ! $relationModel) { |
|
| 359 | 359 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
| 360 | 360 | } |
| 361 | 361 | |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | /** |
| 394 | 394 | * If model doesn't exists. |
| 395 | 395 | */ |
| 396 | - if (! $relationModel) { |
|
| 396 | + if ( ! $relationModel) { |
|
| 397 | 397 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
| 398 | 398 | } |
| 399 | 399 | |
@@ -579,7 +579,7 @@ discard block |
||
| 579 | 579 | } elseif (strtolower($operator) == 'has') { |
| 580 | 580 | $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
| 581 | 581 | $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
| 582 | - if($value) { |
|
| 582 | + if ($value) { |
|
| 583 | 583 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
| 584 | 584 | $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
| 585 | 585 | $conditionValues = array_merge($conditionValues, $bindings); |
@@ -610,7 +610,7 @@ discard block |
||
| 610 | 610 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
| 611 | 611 | $path = explode('->', $value); |
| 612 | 612 | $field = array_shift($path); |
| 613 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 613 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
| 614 | 614 | return '"'.$part.'"'; |
| 615 | 615 | })->implode('.')); |
| 616 | 616 | |
@@ -9,23 +9,23 @@ |
||
| 9 | 9 | class Setting extends Model |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - use SoftDeletes, Translatable; |
|
| 13 | - protected $table = 'settings'; |
|
| 14 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 15 | - protected $hidden = ['deleted_at']; |
|
| 16 | - protected $guarded = ['id', 'key']; |
|
| 17 | - public $fillable = ['name', 'value']; |
|
| 18 | - public $translatable = ['value']; |
|
| 19 | - public $casts = ['value' => 'json']; |
|
| 12 | + use SoftDeletes, Translatable; |
|
| 13 | + protected $table = 'settings'; |
|
| 14 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 15 | + protected $hidden = ['deleted_at']; |
|
| 16 | + protected $guarded = ['id', 'key']; |
|
| 17 | + public $fillable = ['name', 'value']; |
|
| 18 | + public $translatable = ['value']; |
|
| 19 | + public $casts = ['value' => 'json']; |
|
| 20 | 20 | |
| 21 | - public function newCollection(array $models = []) |
|
| 22 | - { |
|
| 23 | - return parent::newCollection($models)->keyBy('key'); |
|
| 24 | - } |
|
| 21 | + public function newCollection(array $models = []) |
|
| 22 | + { |
|
| 23 | + return parent::newCollection($models)->keyBy('key'); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public static function boot() |
|
| 27 | - { |
|
| 28 | - parent::boot(); |
|
| 29 | - Setting::observe(SettingsObserver::class); |
|
| 30 | - } |
|
| 26 | + public static function boot() |
|
| 27 | + { |
|
| 28 | + parent::boot(); |
|
| 29 | + Setting::observe(SettingsObserver::class); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -8,103 +8,103 @@ |
||
| 8 | 8 | |
| 9 | 9 | class RolesTableSeeder extends Seeder |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Run the database seeds. |
|
| 13 | - * |
|
| 14 | - * @return void |
|
| 15 | - */ |
|
| 16 | - public function run() |
|
| 17 | - { |
|
| 18 | - /** |
|
| 19 | - * Create Default roles. |
|
| 20 | - */ |
|
| 21 | - Role::updateOrInsert([ |
|
| 22 | - 'name' => RoleEnum::ADMIN, |
|
| 23 | - ],[ |
|
| 24 | - 'created_at' => \DB::raw('NOW()'), |
|
| 25 | - 'updated_at' => \DB::raw('NOW()') |
|
| 26 | - ]); |
|
| 11 | + /** |
|
| 12 | + * Run the database seeds. |
|
| 13 | + * |
|
| 14 | + * @return void |
|
| 15 | + */ |
|
| 16 | + public function run() |
|
| 17 | + { |
|
| 18 | + /** |
|
| 19 | + * Create Default roles. |
|
| 20 | + */ |
|
| 21 | + Role::updateOrInsert([ |
|
| 22 | + 'name' => RoleEnum::ADMIN, |
|
| 23 | + ],[ |
|
| 24 | + 'created_at' => \DB::raw('NOW()'), |
|
| 25 | + 'updated_at' => \DB::raw('NOW()') |
|
| 26 | + ]); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Create Default user. |
|
| 30 | - */ |
|
| 31 | - AclUser::firstOrCreate([ |
|
| 32 | - 'email' => '[email protected]', |
|
| 33 | - ],[ |
|
| 34 | - 'name' => 'Admin', |
|
| 35 | - 'password' => \Hash::make('123456'), |
|
| 36 | - 'confirmed' => 1, |
|
| 37 | - 'created_at' => \DB::raw('NOW()'), |
|
| 38 | - 'updated_at' => \DB::raw('NOW()') |
|
| 39 | - ]); |
|
| 28 | + /** |
|
| 29 | + * Create Default user. |
|
| 30 | + */ |
|
| 31 | + AclUser::firstOrCreate([ |
|
| 32 | + 'email' => '[email protected]', |
|
| 33 | + ],[ |
|
| 34 | + 'name' => 'Admin', |
|
| 35 | + 'password' => \Hash::make('123456'), |
|
| 36 | + 'confirmed' => 1, |
|
| 37 | + 'created_at' => \DB::raw('NOW()'), |
|
| 38 | + 'updated_at' => \DB::raw('NOW()') |
|
| 39 | + ]); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Assign default users to admin roles. |
|
| 43 | - */ |
|
| 44 | - $adminRoleId = Role::where('name', RoleEnum::ADMIN)->select('id')->first()->id;; |
|
| 45 | - $adminUserId = AclUser::where('email', '[email protected]')->select('id')->first()->id; |
|
| 46 | - \DB::table('role_user')->updateOrInsert([ |
|
| 47 | - 'user_id' => $adminUserId, |
|
| 48 | - 'role_id' => $adminRoleId, |
|
| 49 | - ],[]); |
|
| 41 | + /** |
|
| 42 | + * Assign default users to admin roles. |
|
| 43 | + */ |
|
| 44 | + $adminRoleId = Role::where('name', RoleEnum::ADMIN)->select('id')->first()->id;; |
|
| 45 | + $adminUserId = AclUser::where('email', '[email protected]')->select('id')->first()->id; |
|
| 46 | + \DB::table('role_user')->updateOrInsert([ |
|
| 47 | + 'user_id' => $adminUserId, |
|
| 48 | + 'role_id' => $adminRoleId, |
|
| 49 | + ],[]); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Insert the permissions related to roles table. |
|
| 53 | - */ |
|
| 54 | - \DB::table('permissions')->insert( |
|
| 55 | - [ |
|
| 56 | - /** |
|
| 57 | - * Roles model permissions. |
|
| 58 | - */ |
|
| 59 | - [ |
|
| 60 | - 'name' => 'index', |
|
| 61 | - 'model' => 'role', |
|
| 62 | - 'created_at' => \DB::raw('NOW()'), |
|
| 63 | - 'updated_at' => \DB::raw('NOW()') |
|
| 64 | - ], |
|
| 65 | - [ |
|
| 66 | - 'name' => 'show', |
|
| 67 | - 'model' => 'role', |
|
| 68 | - 'created_at' => \DB::raw('NOW()'), |
|
| 69 | - 'updated_at' => \DB::raw('NOW()') |
|
| 70 | - ], |
|
| 71 | - [ |
|
| 72 | - 'name' => 'store', |
|
| 73 | - 'model' => 'role', |
|
| 74 | - 'created_at' => \DB::raw('NOW()'), |
|
| 75 | - 'updated_at' => \DB::raw('NOW()') |
|
| 76 | - ], |
|
| 77 | - [ |
|
| 78 | - 'name' => 'update', |
|
| 79 | - 'model' => 'role', |
|
| 80 | - 'created_at' => \DB::raw('NOW()'), |
|
| 81 | - 'updated_at' => \DB::raw('NOW()') |
|
| 82 | - ], |
|
| 83 | - [ |
|
| 84 | - 'name' => 'destroy', |
|
| 85 | - 'model' => 'role', |
|
| 86 | - 'created_at' => \DB::raw('NOW()'), |
|
| 87 | - 'updated_at' => \DB::raw('NOW()') |
|
| 88 | - ], |
|
| 89 | - [ |
|
| 90 | - 'name' => 'deleted', |
|
| 91 | - 'model' => 'role', |
|
| 92 | - 'created_at' => \DB::raw('NOW()'), |
|
| 93 | - 'updated_at' => \DB::raw('NOW()') |
|
| 94 | - ], |
|
| 95 | - [ |
|
| 96 | - 'name' => 'restore', |
|
| 97 | - 'model' => 'role', |
|
| 98 | - 'created_at' => \DB::raw('NOW()'), |
|
| 99 | - 'updated_at' => \DB::raw('NOW()') |
|
| 100 | - ], |
|
| 101 | - [ |
|
| 102 | - 'name' => 'assignPermissions', |
|
| 103 | - 'model' => 'role', |
|
| 104 | - 'created_at' => \DB::raw('NOW()'), |
|
| 105 | - 'updated_at' => \DB::raw('NOW()') |
|
| 106 | - ], |
|
| 107 | - ] |
|
| 108 | - ); |
|
| 109 | - } |
|
| 51 | + /** |
|
| 52 | + * Insert the permissions related to roles table. |
|
| 53 | + */ |
|
| 54 | + \DB::table('permissions')->insert( |
|
| 55 | + [ |
|
| 56 | + /** |
|
| 57 | + * Roles model permissions. |
|
| 58 | + */ |
|
| 59 | + [ |
|
| 60 | + 'name' => 'index', |
|
| 61 | + 'model' => 'role', |
|
| 62 | + 'created_at' => \DB::raw('NOW()'), |
|
| 63 | + 'updated_at' => \DB::raw('NOW()') |
|
| 64 | + ], |
|
| 65 | + [ |
|
| 66 | + 'name' => 'show', |
|
| 67 | + 'model' => 'role', |
|
| 68 | + 'created_at' => \DB::raw('NOW()'), |
|
| 69 | + 'updated_at' => \DB::raw('NOW()') |
|
| 70 | + ], |
|
| 71 | + [ |
|
| 72 | + 'name' => 'store', |
|
| 73 | + 'model' => 'role', |
|
| 74 | + 'created_at' => \DB::raw('NOW()'), |
|
| 75 | + 'updated_at' => \DB::raw('NOW()') |
|
| 76 | + ], |
|
| 77 | + [ |
|
| 78 | + 'name' => 'update', |
|
| 79 | + 'model' => 'role', |
|
| 80 | + 'created_at' => \DB::raw('NOW()'), |
|
| 81 | + 'updated_at' => \DB::raw('NOW()') |
|
| 82 | + ], |
|
| 83 | + [ |
|
| 84 | + 'name' => 'destroy', |
|
| 85 | + 'model' => 'role', |
|
| 86 | + 'created_at' => \DB::raw('NOW()'), |
|
| 87 | + 'updated_at' => \DB::raw('NOW()') |
|
| 88 | + ], |
|
| 89 | + [ |
|
| 90 | + 'name' => 'deleted', |
|
| 91 | + 'model' => 'role', |
|
| 92 | + 'created_at' => \DB::raw('NOW()'), |
|
| 93 | + 'updated_at' => \DB::raw('NOW()') |
|
| 94 | + ], |
|
| 95 | + [ |
|
| 96 | + 'name' => 'restore', |
|
| 97 | + 'model' => 'role', |
|
| 98 | + 'created_at' => \DB::raw('NOW()'), |
|
| 99 | + 'updated_at' => \DB::raw('NOW()') |
|
| 100 | + ], |
|
| 101 | + [ |
|
| 102 | + 'name' => 'assignPermissions', |
|
| 103 | + 'model' => 'role', |
|
| 104 | + 'created_at' => \DB::raw('NOW()'), |
|
| 105 | + 'updated_at' => \DB::raw('NOW()') |
|
| 106 | + ], |
|
| 107 | + ] |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | 110 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | Role::updateOrInsert([ |
| 22 | 22 | 'name' => RoleEnum::ADMIN, |
| 23 | - ],[ |
|
| 23 | + ], [ |
|
| 24 | 24 | 'created_at' => \DB::raw('NOW()'), |
| 25 | 25 | 'updated_at' => \DB::raw('NOW()') |
| 26 | 26 | ]); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | AclUser::firstOrCreate([ |
| 32 | 32 | 'email' => '[email protected]', |
| 33 | - ],[ |
|
| 33 | + ], [ |
|
| 34 | 34 | 'name' => 'Admin', |
| 35 | 35 | 'password' => \Hash::make('123456'), |
| 36 | 36 | 'confirmed' => 1, |
@@ -41,12 +41,12 @@ discard block |
||
| 41 | 41 | /** |
| 42 | 42 | * Assign default users to admin roles. |
| 43 | 43 | */ |
| 44 | - $adminRoleId = Role::where('name', RoleEnum::ADMIN)->select('id')->first()->id;; |
|
| 44 | + $adminRoleId = Role::where('name', RoleEnum::ADMIN)->select('id')->first()->id; ; |
|
| 45 | 45 | $adminUserId = AclUser::where('email', '[email protected]')->select('id')->first()->id; |
| 46 | 46 | \DB::table('role_user')->updateOrInsert([ |
| 47 | 47 | 'user_id' => $adminUserId, |
| 48 | 48 | 'role_id' => $adminRoleId, |
| 49 | - ],[]); |
|
| 49 | + ], []); |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * Insert the permissions related to roles table. |
@@ -7,25 +7,25 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Permission extends JsonResource |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Transform the resource into an array. |
|
| 12 | - * |
|
| 13 | - * @param Request $request |
|
| 14 | - * @return array |
|
| 15 | - */ |
|
| 16 | - public function toArray($request) |
|
| 17 | - { |
|
| 18 | - if (! $this->resource) { |
|
| 19 | - return []; |
|
| 20 | - } |
|
| 10 | + /** |
|
| 11 | + * Transform the resource into an array. |
|
| 12 | + * |
|
| 13 | + * @param Request $request |
|
| 14 | + * @return array |
|
| 15 | + */ |
|
| 16 | + public function toArray($request) |
|
| 17 | + { |
|
| 18 | + if (! $this->resource) { |
|
| 19 | + return []; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - return [ |
|
| 23 | - 'id' => $this->id, |
|
| 24 | - 'name' => $this->name, |
|
| 25 | - 'model' => $this->model, |
|
| 26 | - 'roles' => RoleResource::collection($this->whenLoaded('roles')), |
|
| 27 | - 'created_at' => $this->created_at, |
|
| 28 | - 'updated_at' => $this->updated_at, |
|
| 29 | - ]; |
|
| 30 | - } |
|
| 22 | + return [ |
|
| 23 | + 'id' => $this->id, |
|
| 24 | + 'name' => $this->name, |
|
| 25 | + 'model' => $this->model, |
|
| 26 | + 'roles' => RoleResource::collection($this->whenLoaded('roles')), |
|
| 27 | + 'created_at' => $this->created_at, |
|
| 28 | + 'updated_at' => $this->updated_at, |
|
| 29 | + ]; |
|
| 30 | + } |
|
| 31 | 31 | } |