122 lines
3.3 KiB
PHP
122 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
function __construct()
|
|
{
|
|
$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);
|
|
$this->middleware('permission:product-create', ['only' => ['create','store']]);
|
|
$this->middleware('permission:product-edit', ['only' => ['edit','update']]);
|
|
$this->middleware('permission:product-delete', ['only' => ['destroy']]);
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(): View
|
|
{
|
|
$products = Product::latest()->paginate(5);
|
|
|
|
return view('products.index',compact('products'))
|
|
->with('i', (request()->input('page', 1) - 1) * 5);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create(): View
|
|
{
|
|
return view('products.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'detail' => 'required',
|
|
]);
|
|
|
|
Product::create($request->all());
|
|
|
|
return redirect()->route('products.index')
|
|
->with('success','Product created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Product $product
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Product $product): View
|
|
{
|
|
return view('products.show',compact('product'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Product $product
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Product $product): View
|
|
{
|
|
return view('products.edit',compact('product'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\Product $product
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, Product $product): RedirectResponse
|
|
{
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'detail' => 'required',
|
|
]);
|
|
|
|
$product->update($request->all());
|
|
|
|
return redirect()->route('products.index')
|
|
->with('success','Product updated successfully');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Product $product
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
$product->delete();
|
|
|
|
return redirect()->route('products.index')
|
|
->with('success','Product deleted successfully');
|
|
}
|
|
}
|