middleware(function ($request, $next) { $this->account = auth()->user() ->account() ->with('profile') ->first(); return $next($request); }); } public function account(Request $request) { if(!empty($this->account)){ return AccountResource::make($this->account); } return response()->json([ 'message'=> trans('app.account.not_found') ],404 ); } public function storeContacts(ContactsRequest $request){ $contacts = $request->only(array_keys($request->rules())); $this->account->fill(['contacts' => json_encode($contacts)]); if($this->account->save()){ return new ContactResource((object)$contacts); } return response()->json([ 'message'=> trans('app.account.not_found') ],404 ); } public function storeBankAccount(BankAccountRequest $request){ $bank = $request->only(array_keys($request->rules())); $this->account->fill(['bank' => json_encode($bank)]); if($this->account->save()){ return BankResource::make((object)$bank); } return response()->json([ 'message'=> trans('app.account.not_found') ],404 ); } public function storeProfile() { //Profile type using Strategy pattern $type = config('account.'.$this->account->type.'.class'); $profileStrategy = new $type; $profileStrategy->validateRequest(); if($profile = $profileStrategy->updateProfile($this->account->profile)){ return $profile; } return response()->json(['message' => trans('app.account.update_account_error')],400); } public function numbersAvailibility(){ if(!empty($this->account)){ return ApplicationStatusResource::make($this->account); } return response()->json([ 'message'=> trans('app.account.not_found') ],404 ); } }