[COMMON] Lấy tham số đường dẫn http://localhost/test/products?page=3 (ok)
Previous[COMMON] $products = Product::latest()->paginate(5) (ok)Next[COMMON] cách đặt quy luật và bắt lỗi error, validate (ok)
Last updated
Last updated
Mục đánh số thứ tự cho sản phẩm
C:\xampp\htdocs\test\app\Http\Controllers\ProductController.php
C:\xampp\htdocs\test\app\Http\Controllers\ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$products = Product::latest()->paginate(2);
echo '<pre>';
var_export(request()->input('page', 1));
echo '</pre>';
return view('products.index', compact('products'))->with('i', (request()->input('page', 1) - 1) * 2);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
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) {
$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\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product) {
return view('products.show', compact('product'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product) {
return view('products.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product) {
$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\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product) {
$product->delete();
return redirect()->route('products.index')->with('success', 'Product deleted successfully');
}
}