/** * Run the database seeds. * * @return void */ public function run() { DB::table('ratings')->delete(); Rating::create(array('user_id' => '1', 'book_id' => '1', 'rating' => '7')); Rating::create(array('user_id' => '1', 'book_id' => '2', 'rating' => '8')); Rating::create(array('user_id' => '1', 'book_id' => '3', 'rating' => '9')); Rating::create(array('user_id' => '2', 'book_id' => '1', 'rating' => '9')); Rating::create(array('user_id' => '2', 'book_id' => '3', 'rating' => '9')); }
/** * @param Request $request * @return array */ public function store(Request $request) { if (Auth::check()) { $request['user_id'] = Auth::id(); if (!Rating::where('book_id', '=', $request->book_id)->where('user_id', '=', $request->user_id)->exists()) { Rating::create($request->all()); return 1; // Added } return 2; // You have already voted on this Books } else { return 3; // You are not Logged } return $input; }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $result = $this->getAuthenticatedUser(); $value = $result->getData(); if (property_exists($value, 'error')) { return $result; } else { $givenTo = $request->input('userId'); $givenBy = $value->result->id; $ratingValue = $request->input('ratingValue'); try { Rating::create(['givenBy' => $givenBy, 'givenTo' => $givenTo, 'ratingValue' => $ratingValue]); } catch (\Exception $e) { return response()->json(['error' => ['message' => 'Error while saving rating', 'code' => 400]]); } $finalResult['rating'] = $this->ratingForUser($givenTo); return response()->json(['result' => $finalResult]); } }
/** * Run the database seeds. * * @return void */ public function run() { if (App::environment() === 'production') { exit('Do not seed in production environment'); } DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // disable foreign key constraints DB::table('ratings')->truncate(); // Rating::create([ // 'id' => 1, // 'givenBy' => 2, // 'givenTo' => 3, // 'ratingValue' => 5 // ]); Rating::create(['id' => 2, 'givenBy' => 2, 'givenTo' => 4, 'ratingValue' => 3]); Rating::create(['id' => 3, 'givenBy' => 2, 'givenTo' => 5, 'ratingValue' => 4]); Rating::create(['id' => 4, 'givenBy' => 6, 'givenTo' => 5, 'ratingValue' => 3]); Rating::create(['id' => 1, 'givenBy' => 6, 'givenTo' => 3, 'ratingValue' => 5]); DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // enable foreign key constraints }
public function rate($id) { $wbbUId = \Input::get('wbbUId'); $i = explode('_', explode(' ', \Input::get('clickedOn'))[0])[1]; $c = Rating::where('downloadId', $id)->where('WbbUId', $wbbUId)->count(); if ($c > 0) { Rating::where('downloadId', $id)->where('WbbUId', $wbbUId)->update(array('value' => $i)); } else { Rating::create(array('value' => $i, 'downloadId' => $id, 'WbbUId' => $wbbUId)); } return response(array('avg' => intval(round(Rating::getAvg($id))))); }
private function checkRatings($ratings) { $user = Auth::user(); $currentRatings = array_filter($ratings, 'is_numeric'); $newRatings = array_diff($ratings, $currentRatings); foreach ($newRatings as $newRating) { $rating = Rating::create(['name' => $newRating]); $user->ratings()->save($rating); $currentRatings[] = $rating->id; } return $currentRatings; }