Last active
February 11, 2017 19:40
-
-
Save evercode1/330dbba89dd0690a7ab4d95cf1fa9f55 to your computer and use it in GitHub Desktop.
chapter 15 CategoryController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| use App\Category; | |
| use Illuminate\Support\Facades\Redirect; | |
| class CategoryController extends Controller | |
| { | |
| public function __construct() | |
| { | |
| $this->middleware('auth'); | |
| } | |
| /** | |
| * Display a listing of the resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function index() | |
| { | |
| return view('category.index'); | |
| } | |
| /** | |
| * Show the form for creating a new resource. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function create() | |
| { | |
| return view('category.create'); | |
| } | |
| /** | |
| * Store a newly created resource in storage. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function store(Request $request) | |
| { | |
| $this->validate($request, [ | |
| 'name' => 'required|unique:categories|string|max:30', | |
| ]); | |
| $category = Category::create(['name' => $request->name]); | |
| $category->save(); | |
| return Redirect::route('category.index'); | |
| } | |
| /** | |
| * Display the specified resource. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function show($id) | |
| { | |
| $category = Category::findOrFail($id); | |
| return view('category.show', compact('category')); | |
| } | |
| /** | |
| * Show the form for editing the specified resource. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function edit($id) | |
| { | |
| $category = Category::findOrFail($id); | |
| return view('category.edit', compact('category')); | |
| } | |
| /** | |
| * Update the specified resource in storage. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function update(Request $request, $id) | |
| { | |
| $this->validate($request, [ | |
| 'name' => 'required|string|max:40|unique:categories,name,' .$id | |
| ]); | |
| $category = Category::findOrFail($id); | |
| $category->update(['name' => $request->name]); | |
| return Redirect::route('category.show', ['category' => $category]); | |
| } | |
| /** | |
| * Remove the specified resource from storage. | |
| * | |
| * @param int $id | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function destroy($id) | |
| { | |
| Category::destroy($id); | |
| return Redirect::route('category.index'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment