/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $subbreddit = Subbreddit::findOrFail($id); $this->authorize('update-destroy', $subbreddit); $subbreddit->delete(); return $subbreddit; }
/** * 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 }
/** * Run the database seeds. * * @return void */ public function run() { /* Probably needs to be updated to NOT truncate User, so contributors pulling down and going through the ReadMe can first create a user, and then seed (allowing the seed to randomly generate posts, comments and subscriptions for the created user first) */ User::truncate(); Post::truncate(); Comment::truncate(); Subbreddit::truncate(); DB::table('subbreddit_user')->truncate(); $users = factory(User::class, 25)->create(); $users->each(function ($user) { $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
/** * 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; }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { return \App\Subbreddit::with('posts.comments.user')->find($id); }