Ejemplo n.º 1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function favorites(Request $request, $end_user_id)
 {
     $endUser = EndUser::findOrFail($end_user_id);
     if (Auth::user()->userable_type !== 'App\\EndUser' && $endUser->id !== Auth::user()->userable->id) {
         return response('Unauthorised', 401);
     }
     return $endUser->favorites;
 }
Ejemplo n.º 2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $endUser = EndUser::findOrFail($id);
     if ($endUser->id !== Auth::user()->userable->id) {
         return response('Unauthorised', 401);
     }
     $data = $request->all();
     $validator = $this->validator($data, $endUser->user->id);
     if ($validator->fails()) {
         return response(['errors' => $validator->messages()], 400);
     }
     $addresses = $data['addresses'];
     $data = array_except($data, ['password_repeat', 'addresses']);
     $userdata = array_only($data, ['email', 'username', 'password']);
     $enduserdata = array_except($data, ['email', 'username', 'password']);
     // Encrypt password
     if (isset($userdata['password'])) {
         $userdata['password'] = bcrypt($userdata['password']);
     }
     // store
     $endUser->update($enduserdata);
     $endUser->user()->update($userdata);
     // Insert related addresses
     $endUser->user->addresses()->delete();
     foreach ($addresses as $address) {
         $endUser->user->addresses()->create($address);
     }
     return $endUser;
 }