/**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $rating = Rating::findOrFail($id);
     if ($rating['end_user_id'] !== Auth::user()->userable->id) {
         return response('Unauthorized', 401);
     }
     if ($rating['editing_expires_at'] < Carbon::now()) {
         return response('Rating editing expired', 401);
     }
     $data = $request->all();
     $validator = $this->validator($data);
     if ($validator->fails()) {
         return response(['errors' => $validator->messages()], 400);
     }
     // store
     $rating->update($data);
     // Update mastori average rating
     $mastori = Mastori::find($rating['mastori_id']);
     $mastori['avg_rating'] = $mastori->ratings()->avg('rating');
     $mastori->save();
     return $rating;
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $mastori = Mastori::findOrFail($id);
     if ($mastori->id !== Auth::user()->userable->id) {
         return response('Unauthorised', 401);
     }
     $data = $request->all();
     $validator = $this->validator($data, $mastori->user->id);
     if ($validator->fails()) {
         return response(['errors' => $validator->messages()], 400);
     }
     $addresses = $data['addresses'];
     $professions = array_pluck($data['professions'], 'id');
     $areas = isset($data['areas']) ? array_pluck($data['areas'], 'id') : [];
     $data = array_except($data, ['password_repeat', 'addresses', 'professions']);
     $userdata = array_only($data, ['email', 'username', 'password']);
     $mastoridata = array_except($data, ['email', 'username', 'password']);
     $data = array_except($data, ['password_repeat', 'addresses', 'professsions']);
     // Encrypt password
     if (isset($userdata['password'])) {
         $userdata['password'] = bcrypt($userdata['password']);
     }
     // store
     $mastori->update($mastoridata);
     $mastori->user()->update($userdata);
     // Insert related addresses
     $mastori->user->addresses()->delete();
     foreach ($addresses as $address) {
         $mastori->user->addresses()->create($address);
     }
     // Sync professions
     $mastori->professions()->sync($professions);
     //update serving areas
     $mastori->areas()->sync($areas);
     return $mastori->load('professions');
 }