[COMMON] cách đặt quy luật và bắt lỗi error, validate (ok)

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);
    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');
  }
}

C:\xampp\htdocs\test\resources\views\products\create.blade.php

@extends('products.layout') 
@section('content') 
<div class="row">
	<div class="col-lg-12 margin-tb">
		<div class="pull-left">
			<h2>Add New Product</h2>
		</div>
		<div class="pull-right">
			<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
		</div>
	</div>
</div> 
@if ($errors->any()) 
<div class="alert alert-danger">
	<strong>Whoops!</strong> There were some problems with your input.<br><br>
	<ul> 
		@foreach ($errors->all() as $error) 
			<li>{{ $error }}</li> 
		@endforeach 
	</ul>
</div> 
@endif 
<form action="{{ route('products.store') }}" method="POST"> 
	@csrf 
	<div class="row">
		<div class="col-xs-12 col-sm-12 col-md-12">
			<div class="form-group">
				<strong>Name:</strong>
				<input type="text" name="name" class="form-control" placeholder="Name">
			</div>
		</div>
		<div class="col-xs-12 col-sm-12 col-md-12">
			<div class="form-group">
				<strong>Detail:</strong>
				<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
			</div>
		</div>
		<div class="col-xs-12 col-sm-12 col-md-12 text-center">
			<button type="submit" class="btn btn-primary">Submit</button>
		</div>
	</div>
</form> 
@endsection

Last updated