Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- class CategoryController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
- public function index(Request $request)
- {
- $categories = \App\Category::paginate(10);
- $filterKeyword = $request->get('name');
- if ($filterKeyword) {
- $categories = \App\Category::where('name', 'LIKE', "%$filterKeyword%")->paginate(10);
- }
- return view('categories.index', ['categories' => $categories]);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- return view('categories.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $name = $request->get('name');
- $new_category = new \App\Category;
- $new_category->name = $name;
- if ($request->file('image')) {
- $image_path = $request->file('image')->store('category_image', 'public');
- $new_category->image = $image_path;
- }
- $new_category->created_by = \Auth::user()->id;
- $new_category->slug = str_slug($name, '-');
- $new_category->save();
- return redirect()->route('categories.create')->with('status', 'Category successfully created!!!');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $category = \App\Category::findOrFail($id);
- return view('categories.show', ['category' => $category]);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $category_to_edit = \App\Category::findOrFail($id);
- return view('categories.edit', ['category' => $category_to_edit]);
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $category = \App\Category::findOrFail($id);
- \Validator::make($request->all(), [
- "name" => "required|min:3|max:20",
- "image" => "required",
- "slug" => [
- "required",
- Rule::unique("categories")->ignore($category->slug, "slug")
- ]
- ])->validate();
- $name = $request->get('name');
- $slug = $request->get('slug');
- $category->name = $name;
- $category->slug = $slug;
- if ($request->file('image')) {
- if ($category->image && file_exists(storage_path('app/public/'. $category->image))) {
- \Storage::delete('public/'. $category->image);
- }
- $new_image = $request->file('image')->store('category_image', 'public');
- }
- $category->updated_by = \Auth::user()->id;
- $category->slug = str_slug($name);
- $category->save();
- return view('categories.edit', ['id' => $id])->with('status', 'Category Successfully updated');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement