/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $user = \Auth::user(); $subbreddit = App\Subbreddit::find($request->subbreddit_id); $user->subscribedSubbreddits()->attach($subbreddit); // Because we have defined the relationship, the attach assumes that the ID we are giving it is the subbreddit's // So instead of the above lines you could just say... // \Auth::user()->subscribedSubbreddts()->attach($request->subbreddit_id); return $request->subbreddit_id; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $subbreddit = \App\Subbreddit::find($id); if ($subbreddit->user_id == \Auth::user()->id) { $subbreddit->delete(); } else { return response("Unauthorized", 403); } return $subbreddit; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // If we instead said $subbreddit = App\Subbreddit::destroy($id); it would remove the entry from the // database then return TRUE or FALSE, which would be assigned to $subbreddit. So instead we used delete() to // keep the entry available in memory in case we want to undo the delete $subbreddit = App\Subbreddit::find($id); if ($subbreddit->user_id == \Auth::user()->id) { $subbreddit->delete(); // Return the now-deleted subbreddit in case the delete needs to be undone return $subbreddit; } return response("Unauthorized", 403); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $subbreddit = \App\Subbreddit::find($id); // Add authorization, must be owner to destroy if ($subbreddit->user_id == \Auth::user()->id) { $subbreddit->delete(); // or replace find/delete with destroy in one line } else { return response("Unauthorized", 403); } return $subbreddit; // good in case of needing to undo }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $subbreddit = \App\Subbreddit::find($id); $subbreddit->delete(); return $subbreddit; }