| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Http\Controllers;
- use App\Category;
- use Illuminate\Http\Request;
- use File;
- class CategoryController extends Controller
- {
- protected $rules = [
- 'name' => ['required']
- ];
- public function index()
- {
- $categories = Category::orderBy('position')->get(); //->paginate(50);
- return view('categories.index',compact('categories'))
- ->with('i', (request()->input('page', 1) - 1) * 5);
- }
- /**
- * 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)
- {
- $request->validate($this->rules);
- $input = $request->all();
- $input["position"] = $input["position"] != '' ? $input["position"] : 0;
- if(request()->image)
- {
- $file = request()->image;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path()."/files/categories"))
- File::makeDirectory(public_path()."/files/categories");
- $path = public_path('files/categories');
- request()->image->move($path, $filename);
- $input["image"] = $filename;
- }
- Category::create($input);
- return redirect()->route('categories.index')
- ->with('success','Category created successfully.');
- }
- /**
- * Display the specified resource.
- *
- * @param \App\Category $category
- * @return \Illuminate\Http\Response
- */
- public function show(Category $category)
- {
- return view('categories.show',compact('category'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Category $category
- * @return \Illuminate\Http\Response
- */
- public function edit(Category $category)
- {
- return view('categories.edit',compact('category'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Category $category
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, Category $category)
- {
- $request->validate($this->rules);
- $input = $request->all();
- $input["position"] = $input["position"] != '' ? $input["position"] : 0;
- if(request()->image)
- {
- $file = request()->image;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path()."/files/categories"))
- File::makeDirectory(public_path()."/files/categories");
- $path = public_path('files/categories');
- request()->image->move($path, $filename);
- $input["image"] = $filename;
- }
- $category->update($input);
- return redirect()->route('categories.index')
- ->with('success','Category updated successfully');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Category $category
- * @return \Illuminate\Http\Response
- */
- public function destroy(Category $category)
- {
- $category->delete();
- return redirect()->route('categories.index')
- ->with('success','Category deleted successfully');
- }
- }
|