示例#1
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)
 {
     $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;
 }