Пример #1
0
 /**
  *responds to GET /recipes/delete/{id?}
  */
 public function getDoDelete($recipe_id)
 {
     $userId = \Auth::id();
     #find recipe
     $recipe = \P4\Recipe::find($recipe_id);
     #confirm recipe belongs to current user
     #if can't find recipe or doesnt belong to current user redirect
     if (is_null($recipe)) {
         \Session::flash('flash_message', 'recipe not found.');
         return redirect('/');
     } elseif ($userId != $recipe->user_id) {
         \Session::flash('flash_message', 'you cannot delete someone elses recipe');
         return redirect('/');
     }
     #remove tags
     if ($recipe->tags()) {
         $recipe->tags()->detach();
     }
     #remove likes associated with this recipe
     $deleteLikes = \P4\Like::where('recipe_id', $recipe_id)->delete();
     #delete
     $recipe->delete();
     #confirmation
     \Session::flash('flash_message', $recipe->title . ' was deleted.');
     return redirect('/');
 }
Пример #2
0
 public function postEdit(Request $request)
 {
     $recipe = \p4\Recipe::find($request->id);
     if (Auth::id() == $recipe->user_id) {
         $recipe->recipe_name = $request->recipe_name;
         $recipe->honey_type = $request->honey_type;
         $recipe->yeast_type = $request->yeast_type;
         $recipe->difficulty = $request->difficulty;
         $recipe->recipe_text = $request->recipe_text;
         $recipe->save();
         return redirect('/recipes/show/' . $recipe->id);
     } else {
         \Session::flash('flash_message', 'The recipe' . $recipe->recipe_name . ' does not belong to you. You cannot edit it.');
     }
 }