pai-sklep/app/Http/Controllers/CategoryController.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2021-12-16 11:51:47 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::query()->get();
return view("category", ["categories"=>$categories]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
// The server should make sure to serve SVG files with the correct CSP to prevent XSS
'name' => 'required|string|unique:categories',
]);
/*
Once the image is validated , create the name on the image
*/
$cat = new Category();
$cat->name = $validatedData["name"];
$cat->save();
return back();
}
public function edit(Category $cat, Request $request)
{
$validatedData = $request->validate([
// The server should make sure to serve SVG files with the correct CSP to prevent XSS
'name' => 'required|string|unique:categories',
]);
/*
Once the image is validated , create the name on the image
*/
$cat->name = $validatedData["name"];
$cat->save();
return back();
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Image $image
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Category::query()->findOrFail($id)->delete();
return back();
}
}