@@ -2,15 +2,15 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Here goes your error messages. |
|
| 7 | - */ |
|
| 8 | - 'connectionError' => 'خطأ في الاتصال', |
|
| 9 | - 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
| 10 | - 'dbQueryError' => 'خطا في البيانات', |
|
| 11 | - 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
| 12 | - 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
| 13 | - 'notFound' => 'ال :replace المطلوب غير موجود', |
|
| 14 | - 'cannotUploadImage' => 'لا يمكن رفع هذه الصورة' |
|
| 5 | + /** |
|
| 6 | + * Here goes your error messages. |
|
| 7 | + */ |
|
| 8 | + 'connectionError' => 'خطأ في الاتصال', |
|
| 9 | + 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
| 10 | + 'dbQueryError' => 'خطا في البيانات', |
|
| 11 | + 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
| 12 | + 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
| 13 | + 'notFound' => 'ال :replace المطلوب غير موجود', |
|
| 14 | + 'cannotUploadImage' => 'لا يمكن رفع هذه الصورة' |
|
| 15 | 15 | |
| 16 | 16 | ]; |
@@ -9,21 +9,21 @@ |
||
| 9 | 9 | class Setting extends Model |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - use SoftDeletes; |
|
| 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']; |
|
| 12 | + use SoftDeletes; |
|
| 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 | 18 | |
| 19 | - public function newCollection(array $models = []) |
|
| 20 | - { |
|
| 21 | - return parent::newCollection($models)->keyBy('key'); |
|
| 22 | - } |
|
| 19 | + public function newCollection(array $models = []) |
|
| 20 | + { |
|
| 21 | + return parent::newCollection($models)->keyBy('key'); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public static function boot() |
|
| 25 | - { |
|
| 26 | - parent::boot(); |
|
| 27 | - Setting::observe(SettingsObserver::class); |
|
| 28 | - } |
|
| 24 | + public static function boot() |
|
| 25 | + { |
|
| 26 | + parent::boot(); |
|
| 27 | + Setting::observe(SettingsObserver::class); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -9,109 +9,109 @@ |
||
| 9 | 9 | |
| 10 | 10 | class BaseApiController extends Controller |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Array of eager loaded relations. |
|
| 14 | - * |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $relations; |
|
| 12 | + /** |
|
| 13 | + * Array of eager loaded relations. |
|
| 14 | + * |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $relations; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var object |
|
| 21 | - */ |
|
| 22 | - protected $service; |
|
| 19 | + /** |
|
| 20 | + * @var object |
|
| 21 | + */ |
|
| 22 | + protected $service; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Path of the model resource. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $modelResource; |
|
| 24 | + /** |
|
| 25 | + * Path of the model resource. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $modelResource; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Path of the sotre form request. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $storeFormRequest; |
|
| 31 | + /** |
|
| 32 | + * Path of the sotre form request. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $storeFormRequest; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Init new object. |
|
| 40 | - * |
|
| 41 | - * @param mixed $service |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function __construct($service) |
|
| 45 | - { |
|
| 46 | - $this->service = new CachingDecorator($service, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * Init new object. |
|
| 40 | + * |
|
| 41 | + * @param mixed $service |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function __construct($service) |
|
| 45 | + { |
|
| 46 | + $this->service = new CachingDecorator($service, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Fetch all records with relations from storage. |
|
| 51 | - * |
|
| 52 | - * @param Request $request |
|
| 53 | - * @return \Illuminate\Http\Response |
|
| 54 | - */ |
|
| 55 | - public function index(Request $request) |
|
| 56 | - { |
|
| 57 | - return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'), $request->query('trashed'))); |
|
| 58 | - } |
|
| 49 | + /** |
|
| 50 | + * Fetch all records with relations from storage. |
|
| 51 | + * |
|
| 52 | + * @param Request $request |
|
| 53 | + * @return \Illuminate\Http\Response |
|
| 54 | + */ |
|
| 55 | + public function index(Request $request) |
|
| 56 | + { |
|
| 57 | + return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'), $request->query('trashed'))); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Fetch the single object with relations from storage. |
|
| 62 | - * |
|
| 63 | - * @param Request $request |
|
| 64 | - * @param integer $id Id of the requested model. |
|
| 65 | - * @return \Illuminate\Http\Response |
|
| 66 | - */ |
|
| 67 | - public function show(Request $request, $id) |
|
| 68 | - { |
|
| 69 | - return new $this->modelResource($this->service->find($id, $request->relations)); |
|
| 70 | - } |
|
| 60 | + /** |
|
| 61 | + * Fetch the single object with relations from storage. |
|
| 62 | + * |
|
| 63 | + * @param Request $request |
|
| 64 | + * @param integer $id Id of the requested model. |
|
| 65 | + * @return \Illuminate\Http\Response |
|
| 66 | + */ |
|
| 67 | + public function show(Request $request, $id) |
|
| 68 | + { |
|
| 69 | + return new $this->modelResource($this->service->find($id, $request->relations)); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Insert the given model to storage. |
|
| 74 | - * |
|
| 75 | - * @return \Illuminate\Http\Response |
|
| 76 | - */ |
|
| 77 | - public function store() |
|
| 78 | - { |
|
| 79 | - $data = \App::make($this->storeFormRequest)->validated(); |
|
| 80 | - return new $this->modelResource($this->service->save($data)); |
|
| 81 | - } |
|
| 72 | + /** |
|
| 73 | + * Insert the given model to storage. |
|
| 74 | + * |
|
| 75 | + * @return \Illuminate\Http\Response |
|
| 76 | + */ |
|
| 77 | + public function store() |
|
| 78 | + { |
|
| 79 | + $data = \App::make($this->storeFormRequest)->validated(); |
|
| 80 | + return new $this->modelResource($this->service->save($data)); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Update the given model to storage. |
|
| 85 | - * |
|
| 86 | - * @param integer $id |
|
| 87 | - * @return \Illuminate\Http\Response |
|
| 88 | - */ |
|
| 89 | - public function update($id) |
|
| 90 | - { |
|
| 91 | - $data = \App::make($this->storeFormRequest)->validated(); |
|
| 92 | - $data['id'] = $id; |
|
| 93 | - return new $this->modelResource($this->service->save($data)); |
|
| 94 | - } |
|
| 83 | + /** |
|
| 84 | + * Update the given model to storage. |
|
| 85 | + * |
|
| 86 | + * @param integer $id |
|
| 87 | + * @return \Illuminate\Http\Response |
|
| 88 | + */ |
|
| 89 | + public function update($id) |
|
| 90 | + { |
|
| 91 | + $data = \App::make($this->storeFormRequest)->validated(); |
|
| 92 | + $data['id'] = $id; |
|
| 93 | + return new $this->modelResource($this->service->save($data)); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Delete by the given id from storage. |
|
| 98 | - * |
|
| 99 | - * @param integer $id Id of the deleted model. |
|
| 100 | - * @return \Illuminate\Http\Response |
|
| 101 | - */ |
|
| 102 | - public function destroy($id) |
|
| 103 | - { |
|
| 104 | - return new GeneralResource($this->service->delete($id)); |
|
| 105 | - } |
|
| 96 | + /** |
|
| 97 | + * Delete by the given id from storage. |
|
| 98 | + * |
|
| 99 | + * @param integer $id Id of the deleted model. |
|
| 100 | + * @return \Illuminate\Http\Response |
|
| 101 | + */ |
|
| 102 | + public function destroy($id) |
|
| 103 | + { |
|
| 104 | + return new GeneralResource($this->service->delete($id)); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Restore the deleted model. |
|
| 109 | - * |
|
| 110 | - * @param integer $id Id of the restored model. |
|
| 111 | - * @return \Illuminate\Http\Response |
|
| 112 | - */ |
|
| 113 | - public function restore($id) |
|
| 114 | - { |
|
| 115 | - return new GeneralResource($this->service->restore($id)); |
|
| 116 | - } |
|
| 107 | + /** |
|
| 108 | + * Restore the deleted model. |
|
| 109 | + * |
|
| 110 | + * @param integer $id Id of the restored model. |
|
| 111 | + * @return \Illuminate\Http\Response |
|
| 112 | + */ |
|
| 113 | + public function restore($id) |
|
| 114 | + { |
|
| 115 | + return new GeneralResource($this->service->restore($id)); |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -8,565 +8,565 @@ |
||
| 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) |
|
| 84 | - { |
|
| 85 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 86 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 87 | - } |
|
| 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) |
|
| 84 | + { |
|
| 85 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 86 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Save the given model to the storage. |
|
| 91 | - * |
|
| 92 | - * @param array $data |
|
| 93 | - * @return mixed |
|
| 94 | - */ |
|
| 95 | - public function save(array $data) |
|
| 96 | - { |
|
| 97 | - \Session::put('locale', 'all'); |
|
| 98 | - $model = false; |
|
| 99 | - $relations = []; |
|
| 100 | - |
|
| 101 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 89 | + /** |
|
| 90 | + * Save the given model to the storage. |
|
| 91 | + * |
|
| 92 | + * @param array $data |
|
| 93 | + * @return mixed |
|
| 94 | + */ |
|
| 95 | + public function save(array $data) |
|
| 96 | + { |
|
| 97 | + \Session::put('locale', 'all'); |
|
| 98 | + $model = false; |
|
| 99 | + $relations = []; |
|
| 100 | + |
|
| 101 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 102 | 102 | |
| 103 | - $model = $this->prepareModel($data); |
|
| 104 | - $relations = $this->prepareRelations($data, $model); |
|
| 105 | - $model = $this->saveModel($model, $relations); |
|
| 106 | - }); |
|
| 103 | + $model = $this->prepareModel($data); |
|
| 104 | + $relations = $this->prepareRelations($data, $model); |
|
| 105 | + $model = $this->saveModel($model, $relations); |
|
| 106 | + }); |
|
| 107 | 107 | |
| 108 | - if (count($relations)) { |
|
| 109 | - $model->load(...array_keys($relations)); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $model; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Delete record from the storage based on the given |
|
| 117 | - * condition. |
|
| 118 | - * |
|
| 119 | - * @param var $value condition value |
|
| 120 | - * @param string $attribute condition column name |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - public function delete($value, $attribute = 'id') |
|
| 124 | - { |
|
| 125 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 126 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 127 | - $model->delete(); |
|
| 128 | - }); |
|
| 129 | - }); |
|
| 130 | - } |
|
| 108 | + if (count($relations)) { |
|
| 109 | + $model->load(...array_keys($relations)); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $model; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Delete record from the storage based on the given |
|
| 117 | + * condition. |
|
| 118 | + * |
|
| 119 | + * @param var $value condition value |
|
| 120 | + * @param string $attribute condition column name |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + public function delete($value, $attribute = 'id') |
|
| 124 | + { |
|
| 125 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 126 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 127 | + $model->delete(); |
|
| 128 | + }); |
|
| 129 | + }); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Fetch records from the storage based on the given |
|
| 134 | - * id. |
|
| 135 | - * |
|
| 136 | - * @param integer $id |
|
| 137 | - * @param string[] $relations |
|
| 138 | - * @param array $columns |
|
| 139 | - * @return object |
|
| 140 | - */ |
|
| 141 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 142 | - { |
|
| 143 | - return $this->model->with($relations)->find($id, $columns); |
|
| 144 | - } |
|
| 132 | + /** |
|
| 133 | + * Fetch records from the storage based on the given |
|
| 134 | + * id. |
|
| 135 | + * |
|
| 136 | + * @param integer $id |
|
| 137 | + * @param string[] $relations |
|
| 138 | + * @param array $columns |
|
| 139 | + * @return object |
|
| 140 | + */ |
|
| 141 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 142 | + { |
|
| 143 | + return $this->model->with($relations)->find($id, $columns); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Fetch records from the storage based on the given |
|
| 148 | - * condition. |
|
| 149 | - * |
|
| 150 | - * @param array $conditions array of conditions |
|
| 151 | - * @param array $relations |
|
| 152 | - * @param string $sortBy |
|
| 153 | - * @param boolean $desc |
|
| 154 | - * @param array $columns |
|
| 155 | - * @return collection |
|
| 156 | - */ |
|
| 157 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 158 | - { |
|
| 159 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 160 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 161 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Fetch the first record from the storage based on the given |
|
| 166 | - * condition. |
|
| 167 | - * |
|
| 168 | - * @param array $conditions array of conditions |
|
| 169 | - * @param array $relations |
|
| 170 | - * @param array $columns |
|
| 171 | - * @return object |
|
| 172 | - */ |
|
| 173 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 174 | - { |
|
| 175 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 176 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Return the deleted models in pages based on the given conditions. |
|
| 181 | - * |
|
| 182 | - * @param array $conditions array of conditions |
|
| 183 | - * @param integer $perPage |
|
| 184 | - * @param string $sortBy |
|
| 185 | - * @param boolean $desc |
|
| 186 | - * @param array $columns |
|
| 187 | - * @return collection |
|
| 188 | - */ |
|
| 189 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 190 | - { |
|
| 191 | - unset($conditions['page']); |
|
| 192 | - unset($conditions['perPage']); |
|
| 193 | - unset($conditions['sortBy']); |
|
| 194 | - unset($conditions['sort']); |
|
| 195 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 196 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 197 | - $model = $this->model->onlyTrashed(); |
|
| 198 | - |
|
| 199 | - if (count($conditions['conditionValues'])) { |
|
| 200 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Restore the deleted model. |
|
| 208 | - * |
|
| 209 | - * @param integer $id |
|
| 210 | - * @return void |
|
| 211 | - */ |
|
| 212 | - public function restore($id) |
|
| 213 | - { |
|
| 214 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 215 | - |
|
| 216 | - if (! $model) { |
|
| 217 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $model->restore(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Fill the model with the given data. |
|
| 225 | - * |
|
| 226 | - * @param array $data |
|
| 227 | - * |
|
| 228 | - * @return object |
|
| 229 | - */ |
|
| 230 | - public function prepareModel($data) |
|
| 231 | - { |
|
| 232 | - $modelClass = $this->model; |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * If the id is present in the data then select the model for updating, |
|
| 236 | - * else create new model. |
|
| 237 | - * @var array |
|
| 238 | - */ |
|
| 239 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 240 | - if (! $model) { |
|
| 241 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Construct the model object with the given data, |
|
| 246 | - * and if there is a relation add it to relations array, |
|
| 247 | - * then save the model. |
|
| 248 | - */ |
|
| 249 | - foreach ($data as $key => $value) { |
|
| 250 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 251 | - /** |
|
| 252 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 253 | - */ |
|
| 254 | - $model->$key = $value; |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return $model; |
|
| 259 | - } |
|
| 146 | + /** |
|
| 147 | + * Fetch records from the storage based on the given |
|
| 148 | + * condition. |
|
| 149 | + * |
|
| 150 | + * @param array $conditions array of conditions |
|
| 151 | + * @param array $relations |
|
| 152 | + * @param string $sortBy |
|
| 153 | + * @param boolean $desc |
|
| 154 | + * @param array $columns |
|
| 155 | + * @return collection |
|
| 156 | + */ |
|
| 157 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 158 | + { |
|
| 159 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 160 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 161 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Fetch the first record from the storage based on the given |
|
| 166 | + * condition. |
|
| 167 | + * |
|
| 168 | + * @param array $conditions array of conditions |
|
| 169 | + * @param array $relations |
|
| 170 | + * @param array $columns |
|
| 171 | + * @return object |
|
| 172 | + */ |
|
| 173 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 174 | + { |
|
| 175 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 176 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Return the deleted models in pages based on the given conditions. |
|
| 181 | + * |
|
| 182 | + * @param array $conditions array of conditions |
|
| 183 | + * @param integer $perPage |
|
| 184 | + * @param string $sortBy |
|
| 185 | + * @param boolean $desc |
|
| 186 | + * @param array $columns |
|
| 187 | + * @return collection |
|
| 188 | + */ |
|
| 189 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 190 | + { |
|
| 191 | + unset($conditions['page']); |
|
| 192 | + unset($conditions['perPage']); |
|
| 193 | + unset($conditions['sortBy']); |
|
| 194 | + unset($conditions['sort']); |
|
| 195 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 196 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 197 | + $model = $this->model->onlyTrashed(); |
|
| 198 | + |
|
| 199 | + if (count($conditions['conditionValues'])) { |
|
| 200 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Restore the deleted model. |
|
| 208 | + * |
|
| 209 | + * @param integer $id |
|
| 210 | + * @return void |
|
| 211 | + */ |
|
| 212 | + public function restore($id) |
|
| 213 | + { |
|
| 214 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 215 | + |
|
| 216 | + if (! $model) { |
|
| 217 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $model->restore(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Fill the model with the given data. |
|
| 225 | + * |
|
| 226 | + * @param array $data |
|
| 227 | + * |
|
| 228 | + * @return object |
|
| 229 | + */ |
|
| 230 | + public function prepareModel($data) |
|
| 231 | + { |
|
| 232 | + $modelClass = $this->model; |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * If the id is present in the data then select the model for updating, |
|
| 236 | + * else create new model. |
|
| 237 | + * @var array |
|
| 238 | + */ |
|
| 239 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 240 | + if (! $model) { |
|
| 241 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Construct the model object with the given data, |
|
| 246 | + * and if there is a relation add it to relations array, |
|
| 247 | + * then save the model. |
|
| 248 | + */ |
|
| 249 | + foreach ($data as $key => $value) { |
|
| 250 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 251 | + /** |
|
| 252 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 253 | + */ |
|
| 254 | + $model->$key = $value; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return $model; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Prepare related models based on the given data for the given model. |
|
| 263 | - * |
|
| 264 | - * @param array $data |
|
| 265 | - * @param object $model |
|
| 266 | - * |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - public function prepareRelations($data, $model) |
|
| 270 | - { |
|
| 271 | - /** |
|
| 272 | - * Init the relation array |
|
| 273 | - * |
|
| 274 | - * @var array |
|
| 275 | - */ |
|
| 276 | - $relations = []; |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Construct the model object with the given data, |
|
| 280 | - * and if there is a relation add it to relations array, |
|
| 281 | - * then save the model. |
|
| 282 | - */ |
|
| 283 | - foreach ($data as $key => $value) { |
|
| 284 | - /** |
|
| 285 | - * If the attribute is a relation. |
|
| 286 | - */ |
|
| 287 | - $relation = \Str::camel($key); |
|
| 288 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 289 | - /** |
|
| 290 | - * Check if the relation is a collection. |
|
| 291 | - */ |
|
| 292 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 293 | - /** |
|
| 294 | - * If the relation has no value then marke the relation data |
|
| 295 | - * related to the model to be deleted. |
|
| 296 | - */ |
|
| 297 | - if (! $value || ! count($value)) { |
|
| 298 | - $relations[$relation] = 'delete'; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - if (is_array($value)) { |
|
| 302 | - /** |
|
| 303 | - * Loop through the relation data. |
|
| 304 | - */ |
|
| 305 | - foreach ($value as $attr => $val) { |
|
| 306 | - /** |
|
| 307 | - * Get the relation model. |
|
| 308 | - */ |
|
| 309 | - $relationBaseModel = \Core::$relation()->model; |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Check if the relation is a collection. |
|
| 313 | - */ |
|
| 314 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 315 | - /** |
|
| 316 | - * If the id is present in the data then select the relation model for updating, |
|
| 317 | - * else create new model. |
|
| 318 | - */ |
|
| 319 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * If model doesn't exists. |
|
| 323 | - */ |
|
| 324 | - if (! $relationModel) { |
|
| 325 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Loop through the relation attributes. |
|
| 330 | - */ |
|
| 331 | - foreach ($val as $attr => $val) { |
|
| 332 | - /** |
|
| 333 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 334 | - */ |
|
| 335 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 336 | - $relationModel->$attr = $val; |
|
| 337 | - } elseif(gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 338 | - $extra[$attr] = $val; |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - if(isset($extra)) $relationModel->extra = $extra; |
|
| 343 | - $relations[$relation][] = $relationModel; |
|
| 344 | - } else { |
|
| 345 | - /** |
|
| 346 | - * Prevent the sub relations. |
|
| 347 | - */ |
|
| 348 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 349 | - /** |
|
| 350 | - * If the id is present in the data then select the relation model for updating, |
|
| 351 | - * else create new model. |
|
| 352 | - */ |
|
| 353 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * If model doesn't exists. |
|
| 357 | - */ |
|
| 358 | - if (! $relationModel) { |
|
| 359 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 363 | - /** |
|
| 364 | - * Prevent attributes not in the fillable. |
|
| 365 | - */ |
|
| 366 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 367 | - $relationModel->$relationAttribute = $relationValue; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $relations[$relation] = $relationModel; |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $relations; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Save the model with related models. |
|
| 384 | - * |
|
| 385 | - * @param object $model |
|
| 386 | - * @param array $relations |
|
| 387 | - * |
|
| 388 | - * @return object |
|
| 389 | - */ |
|
| 390 | - public function saveModel($model, $relations) |
|
| 391 | - { |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Loop through the relations array. |
|
| 395 | - */ |
|
| 396 | - foreach ($relations as $key => $value) { |
|
| 397 | - /** |
|
| 398 | - * If the relation is marked for delete then delete it. |
|
| 399 | - */ |
|
| 400 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 401 | - $model->$key()->delete(); |
|
| 402 | - } elseif (gettype($value) == 'array') { |
|
| 403 | - /** |
|
| 404 | - * Save the model. |
|
| 405 | - */ |
|
| 406 | - $model->save(); |
|
| 407 | - $ids = []; |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Loop through the relations. |
|
| 411 | - */ |
|
| 412 | - foreach ($value as $val) { |
|
| 413 | - switch (class_basename($model->$key())) { |
|
| 414 | - /** |
|
| 415 | - * If the relation is one to many then update it's foreign key with |
|
| 416 | - * the model id and save it then add its id to ids array to delete all |
|
| 417 | - * relations who's id isn't in the ids array. |
|
| 418 | - */ |
|
| 419 | - case 'HasMany': |
|
| 420 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 421 | - $val->$foreignKeyName = $model->id; |
|
| 422 | - $val->save(); |
|
| 423 | - $ids[] = $val->id; |
|
| 424 | - break; |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * If the relation is many to many then add it's id to the ids array to |
|
| 428 | - * attache these ids to the model. |
|
| 429 | - */ |
|
| 430 | - case 'BelongsToMany': |
|
| 431 | - $extra = $val->extra; |
|
| 432 | - unset($val->extra); |
|
| 433 | - $val->save(); |
|
| 434 | - $ids[$val->id] = $extra ?? []; |
|
| 435 | - break; |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - switch (class_basename($model->$key())) { |
|
| 439 | - /** |
|
| 440 | - * If the relation is one to many then delete all |
|
| 441 | - * relations who's id isn't in the ids array. |
|
| 442 | - */ |
|
| 443 | - case 'HasMany': |
|
| 444 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 445 | - break; |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * If the relation is many to many then |
|
| 449 | - * detach the previous data and attach |
|
| 450 | - * the ids array to the model. |
|
| 451 | - */ |
|
| 452 | - case 'BelongsToMany': |
|
| 453 | - $model->$key()->detach(); |
|
| 454 | - $model->$key()->attach($ids); |
|
| 455 | - break; |
|
| 456 | - } |
|
| 457 | - } else { |
|
| 458 | - switch (class_basename($model->$key())) { |
|
| 459 | - /** |
|
| 460 | - * If the relation is one to one. |
|
| 461 | - */ |
|
| 462 | - case 'HasOne': |
|
| 463 | - /** |
|
| 464 | - * Save the model. |
|
| 465 | - */ |
|
| 466 | - $model->save(); |
|
| 467 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 468 | - $value->$foreignKeyName = $model->id; |
|
| 469 | - $value->save(); |
|
| 470 | - break; |
|
| 471 | - case 'BelongsTo': |
|
| 472 | - /** |
|
| 473 | - * Save the model. |
|
| 474 | - */ |
|
| 475 | - $value->save(); |
|
| 476 | - $model->$key()->associate($value); |
|
| 477 | - break; |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Save the model. |
|
| 484 | - */ |
|
| 485 | - $model->save(); |
|
| 486 | - |
|
| 487 | - return $model; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Build the conditions recursively for the retrieving methods. |
|
| 492 | - * @param array $conditions |
|
| 493 | - * @return array |
|
| 494 | - */ |
|
| 495 | - protected function constructConditions($conditions, $model) |
|
| 496 | - { |
|
| 497 | - $conditionString = ''; |
|
| 498 | - $conditionValues = []; |
|
| 499 | - foreach ($conditions as $key => $value) { |
|
| 500 | - if (Str::contains($key, '->')) { |
|
| 501 | - $key = $this->wrapJsonSelector($key); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - if ($key == 'and') { |
|
| 505 | - $conditions = $this->constructConditions($value, $model); |
|
| 506 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 507 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 508 | - } elseif ($key == 'or') { |
|
| 509 | - $conditions = $this->constructConditions($value, $model); |
|
| 510 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 511 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 512 | - } else { |
|
| 513 | - if (is_array($value)) { |
|
| 514 | - $operator = $value['op']; |
|
| 515 | - if (strtolower($operator) == 'between') { |
|
| 516 | - $value1 = $value['val1']; |
|
| 517 | - $value2 = $value['val2']; |
|
| 518 | - } else { |
|
| 519 | - $value = Arr::get($value, 'val', ''); |
|
| 520 | - } |
|
| 521 | - } else { |
|
| 522 | - $operator = '='; |
|
| 523 | - } |
|
| 261 | + /** |
|
| 262 | + * Prepare related models based on the given data for the given model. |
|
| 263 | + * |
|
| 264 | + * @param array $data |
|
| 265 | + * @param object $model |
|
| 266 | + * |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + public function prepareRelations($data, $model) |
|
| 270 | + { |
|
| 271 | + /** |
|
| 272 | + * Init the relation array |
|
| 273 | + * |
|
| 274 | + * @var array |
|
| 275 | + */ |
|
| 276 | + $relations = []; |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Construct the model object with the given data, |
|
| 280 | + * and if there is a relation add it to relations array, |
|
| 281 | + * then save the model. |
|
| 282 | + */ |
|
| 283 | + foreach ($data as $key => $value) { |
|
| 284 | + /** |
|
| 285 | + * If the attribute is a relation. |
|
| 286 | + */ |
|
| 287 | + $relation = \Str::camel($key); |
|
| 288 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 289 | + /** |
|
| 290 | + * Check if the relation is a collection. |
|
| 291 | + */ |
|
| 292 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 293 | + /** |
|
| 294 | + * If the relation has no value then marke the relation data |
|
| 295 | + * related to the model to be deleted. |
|
| 296 | + */ |
|
| 297 | + if (! $value || ! count($value)) { |
|
| 298 | + $relations[$relation] = 'delete'; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + if (is_array($value)) { |
|
| 302 | + /** |
|
| 303 | + * Loop through the relation data. |
|
| 304 | + */ |
|
| 305 | + foreach ($value as $attr => $val) { |
|
| 306 | + /** |
|
| 307 | + * Get the relation model. |
|
| 308 | + */ |
|
| 309 | + $relationBaseModel = \Core::$relation()->model; |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Check if the relation is a collection. |
|
| 313 | + */ |
|
| 314 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 315 | + /** |
|
| 316 | + * If the id is present in the data then select the relation model for updating, |
|
| 317 | + * else create new model. |
|
| 318 | + */ |
|
| 319 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * If model doesn't exists. |
|
| 323 | + */ |
|
| 324 | + if (! $relationModel) { |
|
| 325 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Loop through the relation attributes. |
|
| 330 | + */ |
|
| 331 | + foreach ($val as $attr => $val) { |
|
| 332 | + /** |
|
| 333 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 334 | + */ |
|
| 335 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 336 | + $relationModel->$attr = $val; |
|
| 337 | + } elseif(gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 338 | + $extra[$attr] = $val; |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + if(isset($extra)) $relationModel->extra = $extra; |
|
| 343 | + $relations[$relation][] = $relationModel; |
|
| 344 | + } else { |
|
| 345 | + /** |
|
| 346 | + * Prevent the sub relations. |
|
| 347 | + */ |
|
| 348 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 349 | + /** |
|
| 350 | + * If the id is present in the data then select the relation model for updating, |
|
| 351 | + * else create new model. |
|
| 352 | + */ |
|
| 353 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * If model doesn't exists. |
|
| 357 | + */ |
|
| 358 | + if (! $relationModel) { |
|
| 359 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 363 | + /** |
|
| 364 | + * Prevent attributes not in the fillable. |
|
| 365 | + */ |
|
| 366 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 367 | + $relationModel->$relationAttribute = $relationValue; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $relations[$relation] = $relationModel; |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $relations; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Save the model with related models. |
|
| 384 | + * |
|
| 385 | + * @param object $model |
|
| 386 | + * @param array $relations |
|
| 387 | + * |
|
| 388 | + * @return object |
|
| 389 | + */ |
|
| 390 | + public function saveModel($model, $relations) |
|
| 391 | + { |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Loop through the relations array. |
|
| 395 | + */ |
|
| 396 | + foreach ($relations as $key => $value) { |
|
| 397 | + /** |
|
| 398 | + * If the relation is marked for delete then delete it. |
|
| 399 | + */ |
|
| 400 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 401 | + $model->$key()->delete(); |
|
| 402 | + } elseif (gettype($value) == 'array') { |
|
| 403 | + /** |
|
| 404 | + * Save the model. |
|
| 405 | + */ |
|
| 406 | + $model->save(); |
|
| 407 | + $ids = []; |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Loop through the relations. |
|
| 411 | + */ |
|
| 412 | + foreach ($value as $val) { |
|
| 413 | + switch (class_basename($model->$key())) { |
|
| 414 | + /** |
|
| 415 | + * If the relation is one to many then update it's foreign key with |
|
| 416 | + * the model id and save it then add its id to ids array to delete all |
|
| 417 | + * relations who's id isn't in the ids array. |
|
| 418 | + */ |
|
| 419 | + case 'HasMany': |
|
| 420 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 421 | + $val->$foreignKeyName = $model->id; |
|
| 422 | + $val->save(); |
|
| 423 | + $ids[] = $val->id; |
|
| 424 | + break; |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * If the relation is many to many then add it's id to the ids array to |
|
| 428 | + * attache these ids to the model. |
|
| 429 | + */ |
|
| 430 | + case 'BelongsToMany': |
|
| 431 | + $extra = $val->extra; |
|
| 432 | + unset($val->extra); |
|
| 433 | + $val->save(); |
|
| 434 | + $ids[$val->id] = $extra ?? []; |
|
| 435 | + break; |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + switch (class_basename($model->$key())) { |
|
| 439 | + /** |
|
| 440 | + * If the relation is one to many then delete all |
|
| 441 | + * relations who's id isn't in the ids array. |
|
| 442 | + */ |
|
| 443 | + case 'HasMany': |
|
| 444 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 445 | + break; |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * If the relation is many to many then |
|
| 449 | + * detach the previous data and attach |
|
| 450 | + * the ids array to the model. |
|
| 451 | + */ |
|
| 452 | + case 'BelongsToMany': |
|
| 453 | + $model->$key()->detach(); |
|
| 454 | + $model->$key()->attach($ids); |
|
| 455 | + break; |
|
| 456 | + } |
|
| 457 | + } else { |
|
| 458 | + switch (class_basename($model->$key())) { |
|
| 459 | + /** |
|
| 460 | + * If the relation is one to one. |
|
| 461 | + */ |
|
| 462 | + case 'HasOne': |
|
| 463 | + /** |
|
| 464 | + * Save the model. |
|
| 465 | + */ |
|
| 466 | + $model->save(); |
|
| 467 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 468 | + $value->$foreignKeyName = $model->id; |
|
| 469 | + $value->save(); |
|
| 470 | + break; |
|
| 471 | + case 'BelongsTo': |
|
| 472 | + /** |
|
| 473 | + * Save the model. |
|
| 474 | + */ |
|
| 475 | + $value->save(); |
|
| 476 | + $model->$key()->associate($value); |
|
| 477 | + break; |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Save the model. |
|
| 484 | + */ |
|
| 485 | + $model->save(); |
|
| 486 | + |
|
| 487 | + return $model; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Build the conditions recursively for the retrieving methods. |
|
| 492 | + * @param array $conditions |
|
| 493 | + * @return array |
|
| 494 | + */ |
|
| 495 | + protected function constructConditions($conditions, $model) |
|
| 496 | + { |
|
| 497 | + $conditionString = ''; |
|
| 498 | + $conditionValues = []; |
|
| 499 | + foreach ($conditions as $key => $value) { |
|
| 500 | + if (Str::contains($key, '->')) { |
|
| 501 | + $key = $this->wrapJsonSelector($key); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + if ($key == 'and') { |
|
| 505 | + $conditions = $this->constructConditions($value, $model); |
|
| 506 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 507 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 508 | + } elseif ($key == 'or') { |
|
| 509 | + $conditions = $this->constructConditions($value, $model); |
|
| 510 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 511 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 512 | + } else { |
|
| 513 | + if (is_array($value)) { |
|
| 514 | + $operator = $value['op']; |
|
| 515 | + if (strtolower($operator) == 'between') { |
|
| 516 | + $value1 = $value['val1']; |
|
| 517 | + $value2 = $value['val2']; |
|
| 518 | + } else { |
|
| 519 | + $value = Arr::get($value, 'val', ''); |
|
| 520 | + } |
|
| 521 | + } else { |
|
| 522 | + $operator = '='; |
|
| 523 | + } |
|
| 524 | 524 | |
| 525 | - if (strtolower($operator) == 'between') { |
|
| 526 | - $conditionString .= $key.' >= ? and '; |
|
| 527 | - $conditionValues[] = $value1; |
|
| 528 | - |
|
| 529 | - $conditionString .= $key.' <= ? {op} '; |
|
| 530 | - $conditionValues[] = $value2; |
|
| 531 | - } elseif (strtolower($operator) == 'in') { |
|
| 532 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 533 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 534 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 535 | - } elseif (strtolower($operator) == 'null') { |
|
| 536 | - $conditionString .= $key.' is null {op} '; |
|
| 537 | - } elseif (strtolower($operator) == 'not null') { |
|
| 538 | - $conditionString .= $key.' is not null {op} '; |
|
| 539 | - } elseif (strtolower($operator) == 'has') { |
|
| 540 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 541 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 542 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 543 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 544 | - } else { |
|
| 545 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 546 | - $conditionValues[] = $value; |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 551 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Wrap the given JSON selector. |
|
| 556 | - * |
|
| 557 | - * @param string $value |
|
| 558 | - * @return string |
|
| 559 | - */ |
|
| 560 | - protected function wrapJsonSelector($value) |
|
| 561 | - { |
|
| 562 | - $removeLast = strpos($value, ')'); |
|
| 563 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 564 | - $path = explode('->', $value); |
|
| 565 | - $field = array_shift($path); |
|
| 566 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 567 | - return '"'.$part.'"'; |
|
| 568 | - })->implode('.')); |
|
| 525 | + if (strtolower($operator) == 'between') { |
|
| 526 | + $conditionString .= $key.' >= ? and '; |
|
| 527 | + $conditionValues[] = $value1; |
|
| 528 | + |
|
| 529 | + $conditionString .= $key.' <= ? {op} '; |
|
| 530 | + $conditionValues[] = $value2; |
|
| 531 | + } elseif (strtolower($operator) == 'in') { |
|
| 532 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 533 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 534 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 535 | + } elseif (strtolower($operator) == 'null') { |
|
| 536 | + $conditionString .= $key.' is null {op} '; |
|
| 537 | + } elseif (strtolower($operator) == 'not null') { |
|
| 538 | + $conditionString .= $key.' is not null {op} '; |
|
| 539 | + } elseif (strtolower($operator) == 'has') { |
|
| 540 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 541 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 542 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 543 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 544 | + } else { |
|
| 545 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 546 | + $conditionValues[] = $value; |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 551 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Wrap the given JSON selector. |
|
| 556 | + * |
|
| 557 | + * @param string $value |
|
| 558 | + * @return string |
|
| 559 | + */ |
|
| 560 | + protected function wrapJsonSelector($value) |
|
| 561 | + { |
|
| 562 | + $removeLast = strpos($value, ')'); |
|
| 563 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 564 | + $path = explode('->', $value); |
|
| 565 | + $field = array_shift($path); |
|
| 566 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 567 | + return '"'.$part.'"'; |
|
| 568 | + })->implode('.')); |
|
| 569 | 569 | |
| 570 | - return $removeLast === false ? $result : $result.')'; |
|
| 571 | - } |
|
| 570 | + return $removeLast === false ? $result : $result.')'; |
|
| 571 | + } |
|
| 572 | 572 | } |
@@ -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 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $model = false; |
| 99 | 99 | $relations = []; |
| 100 | 100 | |
| 101 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 101 | + \DB::transaction(function() use (&$model, &$relations, $data) { |
|
| 102 | 102 | |
| 103 | 103 | $model = $this->prepareModel($data); |
| 104 | 104 | $relations = $this->prepareRelations($data, $model); |
@@ -122,8 +122,8 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function delete($value, $attribute = 'id') |
| 124 | 124 | { |
| 125 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 126 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 125 | + \DB::transaction(function() use ($value, $attribute) { |
|
| 126 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
| 127 | 127 | $model->delete(); |
| 128 | 128 | }); |
| 129 | 129 | }); |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | { |
| 214 | 214 | $model = $this->model->onlyTrashed()->find($id); |
| 215 | 215 | |
| 216 | - if (! $model) { |
|
| 216 | + if ( ! $model) { |
|
| 217 | 217 | \Errors::notFound(class_basename($this->model).' with id : '.$id); |
| 218 | 218 | } |
| 219 | 219 | |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | * @var array |
| 238 | 238 | */ |
| 239 | 239 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 240 | - if (! $model) { |
|
| 240 | + if ( ! $model) { |
|
| 241 | 241 | \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
| 242 | 242 | } |
| 243 | 243 | |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | * If the relation has no value then marke the relation data |
| 295 | 295 | * related to the model to be deleted. |
| 296 | 296 | */ |
| 297 | - if (! $value || ! count($value)) { |
|
| 297 | + if ( ! $value || ! count($value)) { |
|
| 298 | 298 | $relations[$relation] = 'delete'; |
| 299 | 299 | } |
| 300 | 300 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | /** |
| 322 | 322 | * If model doesn't exists. |
| 323 | 323 | */ |
| 324 | - if (! $relationModel) { |
|
| 324 | + if ( ! $relationModel) { |
|
| 325 | 325 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
| 326 | 326 | } |
| 327 | 327 | |
@@ -334,12 +334,12 @@ discard block |
||
| 334 | 334 | */ |
| 335 | 335 | if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
| 336 | 336 | $relationModel->$attr = $val; |
| 337 | - } elseif(gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 337 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 338 | 338 | $extra[$attr] = $val; |
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - if(isset($extra)) $relationModel->extra = $extra; |
|
| 342 | + if (isset($extra)) $relationModel->extra = $extra; |
|
| 343 | 343 | $relations[$relation][] = $relationModel; |
| 344 | 344 | } else { |
| 345 | 345 | /** |
@@ -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 : '.$value['id']); |
| 360 | 360 | } |
| 361 | 361 | |
@@ -563,7 +563,7 @@ discard block |
||
| 563 | 563 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
| 564 | 564 | $path = explode('->', $value); |
| 565 | 565 | $field = array_shift($path); |
| 566 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 566 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
| 567 | 567 | return '"'.$part.'"'; |
| 568 | 568 | })->implode('.')); |
| 569 | 569 | |
@@ -339,7 +339,9 @@ |
||
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - if(isset($extra)) $relationModel->extra = $extra; |
|
| 342 | + if(isset($extra)) { |
|
| 343 | + $relationModel->extra = $extra; |
|
| 344 | + } |
|
| 343 | 345 | $relations[$relation][] = $relationModel; |
| 344 | 346 | } else { |
| 345 | 347 | /** |
@@ -6,194 +6,194 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseService implements BaseServiceInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var object |
|
| 11 | - */ |
|
| 12 | - public $repo; |
|
| 9 | + /** |
|
| 10 | + * @var object |
|
| 11 | + */ |
|
| 12 | + public $repo; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Init new object. |
|
| 16 | - * |
|
| 17 | - * @param mixed $repo |
|
| 18 | - * @return void |
|
| 19 | - */ |
|
| 20 | - public function __construct($repo) |
|
| 21 | - { |
|
| 22 | - $this->repo = $repo; |
|
| 23 | - } |
|
| 14 | + /** |
|
| 15 | + * Init new object. |
|
| 16 | + * |
|
| 17 | + * @param mixed $repo |
|
| 18 | + * @return void |
|
| 19 | + */ |
|
| 20 | + public function __construct($repo) |
|
| 21 | + { |
|
| 22 | + $this->repo = $repo; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Fetch records with relations based on the given params. |
|
| 27 | - * |
|
| 28 | - * @param array $relations |
|
| 29 | - * @param array $conditions |
|
| 30 | - * @param integer $perPage |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @param boolean $trashed |
|
| 34 | - * @return collection |
|
| 35 | - */ |
|
| 36 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 37 | - { |
|
| 38 | - $filters = []; |
|
| 39 | - $translatable = $this->repo->model->translatable ?? []; |
|
| 40 | - foreach ($conditions as $key => $value) { |
|
| 41 | - if($value && in_array($key, $this->repo->model->fillable ?? [])) { |
|
| 42 | - $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 43 | - $isBool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; |
|
| 44 | - $filters[$key] = $isBool ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 25 | + /** |
|
| 26 | + * Fetch records with relations based on the given params. |
|
| 27 | + * |
|
| 28 | + * @param array $relations |
|
| 29 | + * @param array $conditions |
|
| 30 | + * @param integer $perPage |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @param boolean $trashed |
|
| 34 | + * @return collection |
|
| 35 | + */ |
|
| 36 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 37 | + { |
|
| 38 | + $filters = []; |
|
| 39 | + $translatable = $this->repo->model->translatable ?? []; |
|
| 40 | + foreach ($conditions as $key => $value) { |
|
| 41 | + if($value && in_array($key, $this->repo->model->fillable ?? [])) { |
|
| 42 | + $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 43 | + $isBool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; |
|
| 44 | + $filters[$key] = $isBool ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 48 | 48 | |
| 49 | - if ($trashed) { |
|
| 50 | - return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 51 | - } |
|
| 49 | + if ($trashed) { |
|
| 50 | + return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - if (count($filters)) { |
|
| 54 | - return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 55 | - } |
|
| 53 | + if (count($filters)) { |
|
| 54 | + return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 58 | - } |
|
| 57 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Fetch all records with relations from the storage. |
|
| 62 | - * |
|
| 63 | - * @param array $relations |
|
| 64 | - * @param string $sortBy |
|
| 65 | - * @param boolean $desc |
|
| 66 | - * @param array $columns |
|
| 67 | - * @return collection |
|
| 68 | - */ |
|
| 69 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 70 | - { |
|
| 71 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 72 | - } |
|
| 60 | + /** |
|
| 61 | + * Fetch all records with relations from the storage. |
|
| 62 | + * |
|
| 63 | + * @param array $relations |
|
| 64 | + * @param string $sortBy |
|
| 65 | + * @param boolean $desc |
|
| 66 | + * @param array $columns |
|
| 67 | + * @return collection |
|
| 68 | + */ |
|
| 69 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 70 | + { |
|
| 71 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Fetch all records with relations from storage in pages. |
|
| 76 | - * |
|
| 77 | - * @param integer $perPage |
|
| 78 | - * @param array $relations |
|
| 79 | - * @param string $sortBy |
|
| 80 | - * @param boolean $desc |
|
| 81 | - * @param array $columns |
|
| 82 | - * @return collection |
|
| 83 | - */ |
|
| 84 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 85 | - { |
|
| 86 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Fetch all records with relations from storage in pages. |
|
| 76 | + * |
|
| 77 | + * @param integer $perPage |
|
| 78 | + * @param array $relations |
|
| 79 | + * @param string $sortBy |
|
| 80 | + * @param boolean $desc |
|
| 81 | + * @param array $columns |
|
| 82 | + * @return collection |
|
| 83 | + */ |
|
| 84 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 85 | + { |
|
| 86 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Fetch all records with relations based on |
|
| 91 | - * the given condition from storage in pages. |
|
| 92 | - * |
|
| 93 | - * @param array $conditions array of conditions |
|
| 94 | - * @param integer $perPage |
|
| 95 | - * @param array $relations |
|
| 96 | - * @param string $sortBy |
|
| 97 | - * @param boolean $desc |
|
| 98 | - * @param array $columns |
|
| 99 | - * @return collection |
|
| 100 | - */ |
|
| 101 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 102 | - { |
|
| 103 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 104 | - } |
|
| 89 | + /** |
|
| 90 | + * Fetch all records with relations based on |
|
| 91 | + * the given condition from storage in pages. |
|
| 92 | + * |
|
| 93 | + * @param array $conditions array of conditions |
|
| 94 | + * @param integer $perPage |
|
| 95 | + * @param array $relations |
|
| 96 | + * @param string $sortBy |
|
| 97 | + * @param boolean $desc |
|
| 98 | + * @param array $columns |
|
| 99 | + * @return collection |
|
| 100 | + */ |
|
| 101 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 102 | + { |
|
| 103 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 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 | - return $this->repo->save($data); |
|
| 115 | - } |
|
| 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 | + return $this->repo->save($data); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Delete record from the storage based on the given |
|
| 119 | - * condition. |
|
| 120 | - * |
|
| 121 | - * @param var $value condition value |
|
| 122 | - * @param string $attribute condition column name |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function delete($value, $attribute = 'id') |
|
| 126 | - { |
|
| 127 | - return $this->repo->delete($value, $attribute); |
|
| 128 | - } |
|
| 117 | + /** |
|
| 118 | + * Delete record from the storage based on the given |
|
| 119 | + * condition. |
|
| 120 | + * |
|
| 121 | + * @param var $value condition value |
|
| 122 | + * @param string $attribute condition column name |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function delete($value, $attribute = 'id') |
|
| 126 | + { |
|
| 127 | + return $this->repo->delete($value, $attribute); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Fetch records from the storage based on the given |
|
| 132 | - * id. |
|
| 133 | - * |
|
| 134 | - * @param integer $id |
|
| 135 | - * @param array $relations |
|
| 136 | - * @param array $columns |
|
| 137 | - * @return object |
|
| 138 | - */ |
|
| 139 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 140 | - { |
|
| 141 | - return $this->repo->find($id, $relations, $columns); |
|
| 142 | - } |
|
| 130 | + /** |
|
| 131 | + * Fetch records from the storage based on the given |
|
| 132 | + * id. |
|
| 133 | + * |
|
| 134 | + * @param integer $id |
|
| 135 | + * @param array $relations |
|
| 136 | + * @param array $columns |
|
| 137 | + * @return object |
|
| 138 | + */ |
|
| 139 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 140 | + { |
|
| 141 | + return $this->repo->find($id, $relations, $columns); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Fetch records from the storage based on the given |
|
| 146 | - * condition. |
|
| 147 | - * |
|
| 148 | - * @param array $conditions array of conditions |
|
| 149 | - * @param array $relations |
|
| 150 | - * @param string $sortBy |
|
| 151 | - * @param boolean $desc |
|
| 152 | - * @param array $columns |
|
| 153 | - * @return collection |
|
| 154 | - */ |
|
| 155 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 156 | - { |
|
| 157 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 158 | - } |
|
| 144 | + /** |
|
| 145 | + * Fetch records from the storage based on the given |
|
| 146 | + * condition. |
|
| 147 | + * |
|
| 148 | + * @param array $conditions array of conditions |
|
| 149 | + * @param array $relations |
|
| 150 | + * @param string $sortBy |
|
| 151 | + * @param boolean $desc |
|
| 152 | + * @param array $columns |
|
| 153 | + * @return collection |
|
| 154 | + */ |
|
| 155 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 156 | + { |
|
| 157 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Fetch the first record from the storage based on the given |
|
| 162 | - * condition. |
|
| 163 | - * |
|
| 164 | - * @param array $conditions array of conditions |
|
| 165 | - * @param array $relations |
|
| 166 | - * @param array $columns |
|
| 167 | - * @return object |
|
| 168 | - */ |
|
| 169 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 170 | - { |
|
| 171 | - return $this->repo->first($conditions, $relations, $columns); |
|
| 172 | - } |
|
| 160 | + /** |
|
| 161 | + * Fetch the first record from the storage based on the given |
|
| 162 | + * condition. |
|
| 163 | + * |
|
| 164 | + * @param array $conditions array of conditions |
|
| 165 | + * @param array $relations |
|
| 166 | + * @param array $columns |
|
| 167 | + * @return object |
|
| 168 | + */ |
|
| 169 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 170 | + { |
|
| 171 | + return $this->repo->first($conditions, $relations, $columns); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Return the deleted models in pages based on the given conditions. |
|
| 176 | - * |
|
| 177 | - * @param array $conditions array of conditions |
|
| 178 | - * @param integer $perPage |
|
| 179 | - * @param string $sortBy |
|
| 180 | - * @param boolean $desc |
|
| 181 | - * @param array $columns |
|
| 182 | - * @return collection |
|
| 183 | - */ |
|
| 184 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 185 | - { |
|
| 186 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 187 | - } |
|
| 174 | + /** |
|
| 175 | + * Return the deleted models in pages based on the given conditions. |
|
| 176 | + * |
|
| 177 | + * @param array $conditions array of conditions |
|
| 178 | + * @param integer $perPage |
|
| 179 | + * @param string $sortBy |
|
| 180 | + * @param boolean $desc |
|
| 181 | + * @param array $columns |
|
| 182 | + * @return collection |
|
| 183 | + */ |
|
| 184 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 185 | + { |
|
| 186 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Restore the deleted model. |
|
| 191 | - * |
|
| 192 | - * @param integer $id |
|
| 193 | - * @return void |
|
| 194 | - */ |
|
| 195 | - public function restore($id) |
|
| 196 | - { |
|
| 197 | - return $this->repo->restore($id); |
|
| 198 | - } |
|
| 189 | + /** |
|
| 190 | + * Restore the deleted model. |
|
| 191 | + * |
|
| 192 | + * @param integer $id |
|
| 193 | + * @return void |
|
| 194 | + */ |
|
| 195 | + public function restore($id) |
|
| 196 | + { |
|
| 197 | + return $this->repo->restore($id); |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -38,13 +38,13 @@ |
||
| 38 | 38 | $filters = []; |
| 39 | 39 | $translatable = $this->repo->model->translatable ?? []; |
| 40 | 40 | foreach ($conditions as $key => $value) { |
| 41 | - if($value && in_array($key, $this->repo->model->fillable ?? [])) { |
|
| 42 | - $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 41 | + if ($value && in_array($key, $this->repo->model->fillable ?? [])) { |
|
| 42 | + $key = in_array($key, $translatable) ? $key.'->'.(\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 43 | 43 | $isBool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; |
| 44 | 44 | $filters[$key] = $isBool ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 47 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy.'->'.\Session::get('locale') : $sortBy; |
|
| 48 | 48 | |
| 49 | 49 | if ($trashed) { |
| 50 | 50 | return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
@@ -5,30 +5,30 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Settings extends Migration |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Run the migrations. |
|
| 10 | - * |
|
| 11 | - * @return void |
|
| 12 | - */ |
|
| 13 | - public function up() |
|
| 14 | - { |
|
| 15 | - Schema::create('settings', function (Blueprint $table) { |
|
| 16 | - increments('id'); |
|
| 17 | - $table->string('name', 100); |
|
| 18 | - $table->string('key', 100)->unique(); |
|
| 19 | - $table->text('value')->nullable(); |
|
| 20 | - $table->softDeletes(); |
|
| 21 | - $table->timestamps(); |
|
| 22 | - }); |
|
| 23 | - } |
|
| 8 | + /** |
|
| 9 | + * Run the migrations. |
|
| 10 | + * |
|
| 11 | + * @return void |
|
| 12 | + */ |
|
| 13 | + public function up() |
|
| 14 | + { |
|
| 15 | + Schema::create('settings', function (Blueprint $table) { |
|
| 16 | + increments('id'); |
|
| 17 | + $table->string('name', 100); |
|
| 18 | + $table->string('key', 100)->unique(); |
|
| 19 | + $table->text('value')->nullable(); |
|
| 20 | + $table->softDeletes(); |
|
| 21 | + $table->timestamps(); |
|
| 22 | + }); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Reverse the migrations. |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function down() |
|
| 31 | - { |
|
| 32 | - Schema::dropIfExists('settings'); |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * Reverse the migrations. |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function down() |
|
| 31 | + { |
|
| 32 | + Schema::dropIfExists('settings'); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | - Schema::create('settings', function (Blueprint $table) { |
|
| 15 | + Schema::create('settings', function(Blueprint $table) { |
|
| 16 | 16 | increments('id'); |
| 17 | 17 | $table->string('name', 100); |
| 18 | 18 | $table->string('key', 100)->unique(); |
@@ -7,101 +7,101 @@ |
||
| 7 | 7 | |
| 8 | 8 | class CachingDecorator |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - public $service; |
|
| 10 | + /** |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + public $service; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var Cache |
|
| 17 | - */ |
|
| 18 | - protected $cache; |
|
| 15 | + /** |
|
| 16 | + * @var Cache |
|
| 17 | + */ |
|
| 18 | + protected $cache; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - public $modelKey; |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + public $modelKey; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - public $model; |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + public $model; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $modelClass; |
|
| 30 | + /** |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $modelClass; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var mixed |
|
| 37 | - */ |
|
| 38 | - public $cacheConfig; |
|
| 35 | + /** |
|
| 36 | + * @var mixed |
|
| 37 | + */ |
|
| 38 | + public $cacheConfig; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - public $cacheTag; |
|
| 40 | + /** |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + public $cacheTag; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Init new object. |
|
| 47 | - * |
|
| 48 | - * @param string $service |
|
| 49 | - * @param Cache $cache |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function __construct($service, Cache $cache) |
|
| 54 | - { |
|
| 55 | - $this->service = $service; |
|
| 56 | - $this->cache = $cache; |
|
| 57 | - $this->model = $this->service->repo->model; |
|
| 58 | - $this->modelClass = get_class($this->model); |
|
| 59 | - $serviceClass = explode('\\', get_class($this->service)); |
|
| 60 | - $serviceName = end($serviceClass); |
|
| 61 | - $this->cacheTag = lcfirst(substr($serviceName, 0, strpos($serviceName, 'Service'))); |
|
| 62 | - } |
|
| 45 | + /** |
|
| 46 | + * Init new object. |
|
| 47 | + * |
|
| 48 | + * @param string $service |
|
| 49 | + * @param Cache $cache |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function __construct($service, Cache $cache) |
|
| 54 | + { |
|
| 55 | + $this->service = $service; |
|
| 56 | + $this->cache = $cache; |
|
| 57 | + $this->model = $this->service->repo->model; |
|
| 58 | + $this->modelClass = get_class($this->model); |
|
| 59 | + $serviceClass = explode('\\', get_class($this->service)); |
|
| 60 | + $serviceName = end($serviceClass); |
|
| 61 | + $this->cacheTag = lcfirst(substr($serviceName, 0, strpos($serviceName, 'Service'))); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Handle the cache mechanism for the called method |
|
| 66 | - * based the configurations. |
|
| 67 | - * |
|
| 68 | - * @param string $name the called method name |
|
| 69 | - * @param array $arguments the method arguments |
|
| 70 | - * @return object |
|
| 71 | - */ |
|
| 72 | - public function __call($name, $arguments) |
|
| 73 | - { |
|
| 74 | - $this->setCacheConfig($name); |
|
| 64 | + /** |
|
| 65 | + * Handle the cache mechanism for the called method |
|
| 66 | + * based the configurations. |
|
| 67 | + * |
|
| 68 | + * @param string $name the called method name |
|
| 69 | + * @param array $arguments the method arguments |
|
| 70 | + * @return object |
|
| 71 | + */ |
|
| 72 | + public function __call($name, $arguments) |
|
| 73 | + { |
|
| 74 | + $this->setCacheConfig($name); |
|
| 75 | 75 | |
| 76 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
| 77 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
| 78 | - $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
| 79 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
| 80 | - return call_user_func_array([$this->service, $name], $arguments); |
|
| 81 | - }); |
|
| 82 | - } elseif ($this->cacheConfig) { |
|
| 83 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
| 84 | - return call_user_func_array([$this->service, $name], $arguments); |
|
| 85 | - } |
|
| 76 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
| 77 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
| 78 | + $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
| 79 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
| 80 | + return call_user_func_array([$this->service, $name], $arguments); |
|
| 81 | + }); |
|
| 82 | + } elseif ($this->cacheConfig) { |
|
| 83 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
| 84 | + return call_user_func_array([$this->service, $name], $arguments); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - return call_user_func_array([$this->service, $name], $arguments); |
|
| 88 | - } |
|
| 87 | + return call_user_func_array([$this->service, $name], $arguments); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Set cache config based on the called method. |
|
| 92 | - * |
|
| 93 | - * @param string $name |
|
| 94 | - * @return void |
|
| 95 | - */ |
|
| 96 | - private function setCacheConfig($name) |
|
| 97 | - { |
|
| 98 | - $cacheConfig = Arr::get(config('core.cache_config'), $this->cacheTag, false); |
|
| 99 | - $this->cacheConfig = false; |
|
| 90 | + /** |
|
| 91 | + * Set cache config based on the called method. |
|
| 92 | + * |
|
| 93 | + * @param string $name |
|
| 94 | + * @return void |
|
| 95 | + */ |
|
| 96 | + private function setCacheConfig($name) |
|
| 97 | + { |
|
| 98 | + $cacheConfig = Arr::get(config('core.cache_config'), $this->cacheTag, false); |
|
| 99 | + $this->cacheConfig = false; |
|
| 100 | 100 | |
| 101 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
| 102 | - $this->cacheConfig = 'cache'; |
|
| 103 | - } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
| 104 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 105 | - } |
|
| 106 | - } |
|
| 101 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
| 102 | + $this->cacheConfig = 'cache'; |
|
| 103 | + } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
| 104 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
| 77 | 77 | $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
| 78 | 78 | $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
| 79 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
| 79 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
| 80 | 80 | return call_user_func_array([$this->service, $name], $arguments); |
| 81 | 81 | }); |
| 82 | 82 | } elseif ($this->cacheConfig) { |
@@ -4,70 +4,70 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Media |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Upload the given image. |
|
| 9 | - * |
|
| 10 | - * @param object $image |
|
| 11 | - * @param string $dir |
|
| 12 | - * @return string |
|
| 13 | - */ |
|
| 14 | - public function uploadImage($image, $dir) |
|
| 15 | - { |
|
| 16 | - $image = \Image::make($image); |
|
| 17 | - return $this->saveImage($image, $dir); |
|
| 18 | - } |
|
| 7 | + /** |
|
| 8 | + * Upload the given image. |
|
| 9 | + * |
|
| 10 | + * @param object $image |
|
| 11 | + * @param string $dir |
|
| 12 | + * @return string |
|
| 13 | + */ |
|
| 14 | + public function uploadImage($image, $dir) |
|
| 15 | + { |
|
| 16 | + $image = \Image::make($image); |
|
| 17 | + return $this->saveImage($image, $dir); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Upload the given image. |
|
| 22 | - * |
|
| 23 | - * @param object $image |
|
| 24 | - * @param string $dir |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function uploadImageBas64($image, $dir) |
|
| 28 | - { |
|
| 29 | - if (! strlen($image)) { |
|
| 30 | - return null; |
|
| 31 | - } |
|
| 20 | + /** |
|
| 21 | + * Upload the given image. |
|
| 22 | + * |
|
| 23 | + * @param object $image |
|
| 24 | + * @param string $dir |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function uploadImageBas64($image, $dir) |
|
| 28 | + { |
|
| 29 | + if (! strlen($image)) { |
|
| 30 | + return null; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - if(filter_var($image, FILTER_VALIDATE_URL)) { |
|
| 34 | - return $image; |
|
| 35 | - } |
|
| 33 | + if(filter_var($image, FILTER_VALIDATE_URL)) { |
|
| 34 | + return $image; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - try { |
|
| 38 | - $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
| 39 | - $image = \Image::make($base); |
|
| 40 | - } catch (\Exception $e) { |
|
| 41 | - \Errors::cannotUploadImage(); |
|
| 42 | - } |
|
| 37 | + try { |
|
| 38 | + $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
| 39 | + $image = \Image::make($base); |
|
| 40 | + } catch (\Exception $e) { |
|
| 41 | + \Errors::cannotUploadImage(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return $this->saveImage($image, $dir); |
|
| 45 | - } |
|
| 44 | + return $this->saveImage($image, $dir); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Delete the given image. |
|
| 49 | - * |
|
| 50 | - * @param object $path |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public function deleteImage($path) |
|
| 54 | - { |
|
| 55 | - \Storage::delete($path); |
|
| 56 | - } |
|
| 47 | + /** |
|
| 48 | + * Delete the given image. |
|
| 49 | + * |
|
| 50 | + * @param object $path |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public function deleteImage($path) |
|
| 54 | + { |
|
| 55 | + \Storage::delete($path); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Save the given image. |
|
| 60 | - * |
|
| 61 | - * @param object $image |
|
| 62 | - * @param string $dir |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - protected function saveImage($image, $dir) |
|
| 66 | - { |
|
| 67 | - $imageName = 'image'.uniqid().time().'.jpg'; |
|
| 68 | - $path = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName; |
|
| 69 | - \Storage::put($path, $image->stream()); |
|
| 58 | + /** |
|
| 59 | + * Save the given image. |
|
| 60 | + * |
|
| 61 | + * @param object $image |
|
| 62 | + * @param string $dir |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + protected function saveImage($image, $dir) |
|
| 66 | + { |
|
| 67 | + $imageName = 'image'.uniqid().time().'.jpg'; |
|
| 68 | + $path = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName; |
|
| 69 | + \Storage::put($path, $image->stream()); |
|
| 70 | 70 | |
| 71 | - return $path; |
|
| 72 | - } |
|
| 71 | + return $path; |
|
| 72 | + } |
|
| 73 | 73 | } |
@@ -26,11 +26,11 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | public function uploadImageBas64($image, $dir) |
| 28 | 28 | { |
| 29 | - if (! strlen($image)) { |
|
| 29 | + if ( ! strlen($image)) { |
|
| 30 | 30 | return null; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - if(filter_var($image, FILTER_VALIDATE_URL)) { |
|
| 33 | + if (filter_var($image, FILTER_VALIDATE_URL)) { |
|
| 34 | 34 | return $image; |
| 35 | 35 | } |
| 36 | 36 | |
@@ -13,10 +13,10 @@ |
||
| 13 | 13 | |
| 14 | 14 | Route::group(['prefix' => 'settings'], function () { |
| 15 | 15 | |
| 16 | - Route::get('/', 'SettingController@index'); |
|
| 17 | - Route::get('{id}', 'SettingController@show'); |
|
| 18 | - Route::patch('{id}', 'SettingController@update'); |
|
| 19 | - Route::delete('{id}', 'SettingController@destroy'); |
|
| 20 | - Route::patch('restore/{id}', 'SettingController@restore'); |
|
| 21 | - Route::post('save/many', 'SettingController@saveMany'); |
|
| 16 | + Route::get('/', 'SettingController@index'); |
|
| 17 | + Route::get('{id}', 'SettingController@show'); |
|
| 18 | + Route::patch('{id}', 'SettingController@update'); |
|
| 19 | + Route::delete('{id}', 'SettingController@destroy'); |
|
| 20 | + Route::patch('restore/{id}', 'SettingController@restore'); |
|
| 21 | + Route::post('save/many', 'SettingController@saveMany'); |
|
| 22 | 22 | }); |