Laravel Eloquent nhận giá trị tối thiểu

Hướng dẫn laravel này là về các hàm tổng hợp mô hình hùng hồn. Chúng tôi sẽ sử dụng các tập hợp hùng hồn để đếm dữ liệu bảng cơ sở dữ liệu, tổng, tối đa, tối thiểu và tìm giá trị trung bình

Show

Các hàm tổng hợp mô hình hùng hồn của Laravel

  1. trung bình()
  2. Tổng()
  3. phút()
  4. tối đa()
  5. đếm()

Phương thức avg() hùng hồn của Laravel

Tính giá trị trung bình của một cột bằng phương thức avg() của laravel. hãy lấy một ví dụ về hàm tổng hợp trung bình hùng hồn. ở đây chúng tôi sử dụng một phương thức avg() đơn giản trên mô hình Sinh viên và chuyển tên cột 'marks' nó cho giá trị điểm trung bình của tất cả sinh viên

Phương thức tổng () hùng hồn của Laravel

Cách tính tổng tất cả các giá trị của một cột trong laravel hơn là sử dụng sum(). giải thích thêm bằng một ví dụ lấy mô hình Nhân viên và tính tổng tiền lương của tất cả nhân viên bằng cách lấy cột lương theo phương pháp tổng hùng hồn

Phương thức min() hùng hồn của Laravel

Cách tính giá trị nhỏ nhất của một thuộc tính so với sử dụng hàm tổng hợp tối thiểu hùng hồn. giải thích thêm bằng một ví dụ lấy mô hình Nhân viên và tính mức lương tối thiểu của eloloyees theo phương pháp tối thiểu

Phương thức max() hùng hồn của Laravel

Hàm tổng hợp tối đa hùng hồn của Laravel hoạt động giống như hàm tối thiểu. Nó nhận được giá trị tối đa của cột đã cho. Hãy giải thích thêm bằng ví dụ, trong ví dụ trả về giá trị tiền lương tối đa trong mô hình Nhân viên

Phương thức đếm () hùng hồn của Laravel

Hàm đếm eloquent của Laravel sử dụng để đếm dữ liệu mô hình của các hàng trong bảng. Chúng tôi sử dụng phương pháp đếm đơn giản với một mô hình và nó cho kết quả đếm. Hãy để một ví dụ

count();

    dd($employee);
  }
}

cũng đọc tất cả hàm tổng hợp trong trình tạo truy vấn laravel

Bạn đọc hướng dẫn này trên hướng dẫn web nâng cao. ở đây chúng tôi cung cấp cho người mới bắt đầu laravel hướng dẫn nâng cao

Laravel Eloquent nhận giá trị tối thiểu

Anmol Sharma

Tôi là một kỹ sư phần mềm và có kinh nghiệm về các công nghệ phát triển web như php, Laravel, Codeigniter, javascript, jquery, bootstrap và tôi muốn chia sẻ kiến ​​thức sâu rộng của mình qua các blog này

Laravel cung cấp một số cách tiếp cận khác nhau để xác thực dữ liệu đến của ứng dụng của bạn. Cách phổ biến nhất là sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 có sẵn trên tất cả các yêu cầu HTTP đến. Tuy nhiên, chúng tôi cũng sẽ thảo luận về các cách tiếp cận khác để xác nhận

Laravel bao gồm nhiều quy tắc xác thực thuận tiện mà bạn có thể áp dụng cho dữ liệu, thậm chí cung cấp khả năng xác thực nếu các giá trị là duy nhất trong một bảng cơ sở dữ liệu nhất định. Chúng tôi sẽ trình bày chi tiết từng quy tắc xác thực này để bạn quen thuộc với tất cả các tính năng xác thực của Laravel

Bắt đầu nhanh xác thực

Để tìm hiểu về các tính năng xác thực mạnh mẽ của Laravel, hãy xem một ví dụ hoàn chỉnh về xác thực biểu mẫu và hiển thị lại thông báo lỗi cho người dùng. Bằng cách đọc phần tổng quan cấp cao này, bạn sẽ có thể hiểu rõ hơn về cách xác thực dữ liệu yêu cầu gửi đến bằng cách sử dụng Laravel

Xác định các tuyến đường

Trước tiên, giả sử chúng ta có các tuyến sau được xác định trong tệp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

22 của mình

use App\Http\Controllers\PostController;

Route::get('/post/create', [PostController::class, 'create']);

Route::post('/post', [PostController::class, 'store']);

Lộ trình

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

23 sẽ hiển thị một biểu mẫu để người dùng tạo một bài đăng blog mới, trong khi lộ trình

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

24 sẽ lưu trữ bài đăng blog mới trong cơ sở dữ liệu

Tạo bộ điều khiển

Tiếp theo, chúng ta hãy xem một bộ điều khiển đơn giản xử lý các yêu cầu đến các tuyến này. Bây giờ chúng ta sẽ để trống phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

25

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

Viết logic xác thực

Bây giờ chúng ta đã sẵn sàng để điền logic vào phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

25 của mình để xác thực bài đăng blog mới. Để làm điều này, chúng ta sẽ sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 được cung cấp bởi đối tượng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

28. Nếu các quy tắc xác thực vượt qua, mã của bạn sẽ tiếp tục thực thi bình thường;

Nếu xác thực không thành công trong yêu cầu HTTP truyền thống, phản hồi chuyển hướng tới URL trước đó sẽ được tạo. Nếu yêu cầu đến là yêu cầu XHR, a sẽ được trả lại

Để hiểu rõ hơn về phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21, hãy quay lại phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

25

8

Như bạn có thể thấy, các quy tắc xác thực được chuyển vào phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21. Đừng lo lắng - tất cả các quy tắc xác thực có sẵn đều. Một lần nữa, nếu xác thực không thành công, phản hồi thích hợp sẽ tự động được tạo. Nếu vượt qua xác thực, bộ điều khiển của chúng tôi sẽ tiếp tục thực hiện bình thường

Ngoài ra, các quy tắc xác thực có thể được chỉ định dưới dạng các mảng quy tắc thay vì một chuỗi phân cách

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33

1

Ngoài ra, bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

34 để xác thực yêu cầu và lưu trữ bất kỳ thông báo lỗi nào trong một

3

Dừng khi xác nhận lần đầu không thành công

Đôi khi, bạn có thể muốn ngừng chạy các quy tắc xác thực trên một thuộc tính sau lần xác thực đầu tiên không thành công. Để làm như vậy, hãy gán quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

35 cho thuộc tính

5

Trong ví dụ này, nếu quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

36 trên thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

37 không thành công, quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

38 sẽ không được chọn. Các quy tắc sẽ được xác thực theo thứ tự chúng được chỉ định

Lưu ý về các thuộc tính lồng nhau

Nếu yêu cầu HTTP đến chứa dữ liệu trường "lồng nhau", bạn có thể chỉ định các trường này trong quy tắc xác thực của mình bằng cú pháp "dấu chấm"

9

Mặt khác, nếu tên trường của bạn chứa dấu chấm bằng chữ, bạn có thể ngăn điều này một cách rõ ràng để không bị hiểu là cú pháp "dấu chấm" bằng cách thoát dấu chấm bằng dấu gạch chéo ngược

0

Hiển thị lỗi xác thực

Vì vậy, điều gì sẽ xảy ra nếu các trường yêu cầu đến không vượt qua các quy tắc xác thực đã cho? . Ngoài ra, tất cả các lỗi xác thực và sẽ tự động được

Một biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 được chia sẻ với tất cả các chế độ xem ứng dụng của bạn bởi phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

40, được cung cấp bởi nhóm phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

41. Khi phần mềm trung gian này được áp dụng, một biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 sẽ luôn có sẵn trong chế độ xem của bạn, cho phép bạn giả định một cách thuận tiện rằng biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 luôn được xác định và có thể được sử dụng một cách an toàn. Biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 sẽ là một thể hiện của

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

45. Để biết thêm thông tin về cách làm việc với đối tượng này,

Vì vậy, trong ví dụ của chúng tôi, người dùng sẽ được chuyển hướng đến phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

46 của bộ điều khiển của chúng tôi khi xác thực không thành công, cho phép chúng tôi hiển thị thông báo lỗi trong chế độ xem

9

Tùy chỉnh Thông báo Lỗi

Mỗi quy tắc xác thực tích hợp của Laravel đều có một thông báo lỗi nằm trong tệp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

47 của ứng dụng của bạn. Trong tệp này, bạn sẽ tìm thấy mục dịch cho từng quy tắc xác thực. Bạn có thể tự do thay đổi hoặc sửa đổi các thông báo này dựa trên nhu cầu của ứng dụng của bạn

Ngoài ra, bạn có thể sao chép tệp này sang thư mục ngôn ngữ dịch thuật khác để dịch các thông báo cho ngôn ngữ của ứng dụng của bạn. Để tìm hiểu thêm về bản địa hóa Laravel, hãy xem tài liệu bản địa hóa đầy đủ

Yêu cầu & Xác thực XHR

Trong ví dụ này, chúng tôi đã sử dụng một biểu mẫu truyền thống để gửi dữ liệu đến ứng dụng. Tuy nhiên, nhiều ứng dụng nhận được yêu cầu XHR từ giao diện người dùng được hỗ trợ bởi JavaScript. Khi sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 trong một yêu cầu XHR, Laravel sẽ không tạo phản hồi chuyển hướng. Thay vào đó, Laravel tạo ra một. Phản hồi JSON này sẽ được gửi cùng với mã trạng thái HTTP 422

Chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

49

Bạn có thể sử dụng chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

49 Blade để nhanh chóng xác định xem có tồn tại thông báo lỗi xác thực cho một thuộc tính nhất định hay không. Trong một chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

49, bạn có thể lặp lại biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

52 để hiển thị thông báo lỗi

count();

    dd($employee);
  }
}
6

Nếu bạn đang sử dụng , bạn có thể chuyển tên của túi lỗi làm đối số thứ hai cho chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

49

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

0

Hình thức tái tạo

Khi Laravel tạo phản hồi chuyển hướng do lỗi xác thực, khung sẽ tự động. Điều này được thực hiện để bạn có thể truy cập thông tin đầu vào một cách thuận tiện trong yêu cầu tiếp theo và điền lại biểu mẫu mà người dùng đã cố gắng gửi

Để truy xuất đầu vào được flash từ yêu cầu trước đó, hãy gọi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

54 trên phiên bản của

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

28. Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

54 sẽ lấy dữ liệu đầu vào được flash trước đó từ phiên

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

1

Laravel cũng cung cấp một trình trợ giúp toàn cầu

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

54. Nếu bạn đang hiển thị đầu vào cũ trong mẫu Blade, sẽ thuận tiện hơn khi sử dụng trình trợ giúp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

54 để điền lại biểu mẫu. Nếu không có đầu vào cũ nào tồn tại cho trường đã cho, thì

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 sẽ được trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

0

Lưu ý về các trường tùy chọn

Theo mặc định, Laravel bao gồm phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

60 và

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

61 trong ngăn xếp phần mềm trung gian toàn cầu của ứng dụng của bạn. Các phần mềm trung gian này được liệt kê trong ngăn xếp theo lớp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

62. Do đó, bạn sẽ thường cần đánh dấu các trường yêu cầu "tùy chọn" của mình là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

63 nếu bạn không muốn trình xác thực coi giá trị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 là không hợp lệ. Ví dụ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

3

Trong ví dụ này, chúng tôi chỉ định rằng trường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

65 có thể là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 hoặc biểu diễn ngày hợp lệ. Nếu công cụ sửa đổi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

63 không được thêm vào định nghĩa quy tắc, trình xác thực sẽ coi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 là một ngày không hợp lệ

Định dạng phản hồi lỗi xác thực

Khi ứng dụng của bạn đưa ra một ngoại lệ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

29 và yêu cầu HTTP đến đang chờ phản hồi JSON, Laravel sẽ tự động định dạng thông báo lỗi cho bạn và trả về phản hồi HTTP

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

70

Dưới đây, bạn có thể xem lại một ví dụ về định dạng phản hồi JSON cho các lỗi xác thực. Lưu ý rằng các khóa lỗi lồng nhau được làm phẳng thành định dạng ký hiệu "dấu chấm"

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

4

Xác nhận yêu cầu biểu mẫu

Tạo yêu cầu biểu mẫu

Đối với các tình huống xác thực phức tạp hơn, bạn có thể muốn tạo một "yêu cầu biểu mẫu". Yêu cầu biểu mẫu là các lớp yêu cầu tùy chỉnh đóng gói logic xác thực và ủy quyền của riêng chúng. Để tạo một lớp yêu cầu biểu mẫu, bạn có thể sử dụng lệnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

71 Artisan CLI

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

5

Lớp yêu cầu biểu mẫu được tạo sẽ được đặt trong thư mục

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

72. Nếu thư mục này không tồn tại, nó sẽ được tạo khi bạn chạy lệnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

71. Mỗi yêu cầu biểu mẫu được tạo bởi Laravel có hai phương thức.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74 và

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

75

Như bạn có thể đoán, phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74 chịu trách nhiệm xác định xem người dùng hiện được xác thực có thể thực hiện hành động được yêu cầu thể hiện hay không, trong khi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

75 trả về các quy tắc xác thực sẽ áp dụng cho dữ liệu của yêu cầu

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

6

Lưu ý
Bạn có thể nhập gợi ý bất kỳ phụ thuộc nào bạn yêu cầu trong chữ ký của phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

75. Chúng sẽ tự động được giải quyết thông qua Laravel service container.

Vì vậy, các quy tắc xác nhận được đánh giá như thế nào? . Yêu cầu biểu mẫu đến được xác thực trước khi phương thức bộ điều khiển được gọi, nghĩa là bạn không cần phải làm lộn xộn bộ điều khiển của mình với bất kỳ logic xác thực nào

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

7

Nếu xác thực không thành công, phản hồi chuyển hướng sẽ được tạo để đưa người dùng trở lại vị trí trước đó của họ. Các lỗi cũng sẽ được flash vào phiên để chúng có sẵn để hiển thị. Nếu yêu cầu là yêu cầu XHR, phản hồi HTTP có mã trạng thái 422 sẽ được trả lại cho người dùng bao gồm

Thêm After Hook vào Form Request

Nếu bạn muốn thêm hook xác thực "after" vào yêu cầu biểu mẫu, bạn có thể sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

79. Phương thức này nhận được trình xác thực được xây dựng đầy đủ, cho phép bạn gọi bất kỳ phương thức nào của nó trước khi các quy tắc xác thực thực sự được đánh giá

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

8

Dừng trên thuộc tính lỗi xác thực đầu tiên

Bằng cách thêm thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

80 vào lớp yêu cầu của bạn, bạn có thể thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

9

Tùy chỉnh vị trí chuyển hướng

Như đã thảo luận trước đó, phản hồi chuyển hướng sẽ được tạo để gửi người dùng trở lại vị trí trước đó của họ khi xác thực yêu cầu biểu mẫu không thành công. Tuy nhiên, bạn có thể tự do tùy chỉnh hành vi này. Để làm như vậy, hãy xác định thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

81 theo yêu cầu biểu mẫu của bạn

80

Hoặc, nếu bạn muốn chuyển hướng người dùng đến một tuyến đường đã đặt tên, thay vào đó, bạn có thể xác định thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

82

81

Yêu cầu biểu mẫu ủy quyền

Lớp yêu cầu biểu mẫu cũng chứa một phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74. Trong phương pháp này, bạn có thể xác định xem người dùng được xác thực có thực sự có quyền cập nhật một tài nguyên nhất định hay không. Ví dụ: bạn có thể xác định xem người dùng có thực sự sở hữu nhận xét blog mà họ đang cố cập nhật hay không. Rất có thể, bạn sẽ tương tác với các cổng và chính sách ủy quyền của mình trong phương pháp này

82

Vì tất cả các yêu cầu biểu mẫu mở rộng lớp yêu cầu Laravel cơ sở, chúng tôi có thể sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

84 để truy cập người dùng hiện được xác thực. Ngoài ra, hãy lưu ý lệnh gọi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

85 trong ví dụ trên. Phương thức này cấp cho bạn quyền truy cập vào các tham số URI được xác định trên tuyến đang được gọi, chẳng hạn như tham số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

86 trong ví dụ bên dưới

83

Do đó, nếu ứng dụng của bạn đang tận dụng lợi thế của , mã của bạn có thể được làm ngắn gọn hơn nữa bằng cách truy cập vào mô hình đã giải quyết dưới dạng thuộc tính của yêu cầu

84

Nếu phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74 trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88, phản hồi HTTP có mã trạng thái 403 sẽ tự động được trả về và phương thức điều khiển của bạn sẽ không thực thi

Nếu bạn định xử lý logic ủy quyền cho yêu cầu trong một phần khác của ứng dụng, bạn có thể chỉ cần trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89 từ phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74

85

Lưu ý
Bạn có thể nhập gợi ý bất kỳ phụ thuộc nào bạn cần trong chữ ký của phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

74. Chúng sẽ tự động được giải quyết thông qua Laravel service container.

Tùy chỉnh Thông báo Lỗi

Bạn có thể tùy chỉnh các thông báo lỗi được sử dụng bởi yêu cầu biểu mẫu bằng cách ghi đè phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

92. Phương thức này sẽ trả về một mảng các cặp thuộc tính/quy tắc và các thông báo lỗi tương ứng của chúng

86

Tùy chỉnh các thuộc tính xác thực

Nhiều thông báo lỗi quy tắc xác thực tích hợp sẵn của Laravel chứa trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93. Nếu bạn muốn thay thế trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93 của thông báo xác thực bằng tên thuộc tính tùy chỉnh, bạn có thể chỉ định tên tùy chỉnh bằng cách ghi đè phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

95. Phương thức này sẽ trả về một mảng các cặp thuộc tính/tên

87

Chuẩn bị đầu vào để xác thực

Nếu bạn cần chuẩn bị hoặc làm sạch bất kỳ dữ liệu nào từ yêu cầu trước khi áp dụng các quy tắc xác thực của mình, bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

96

88

Tạo thủ công Trình xác thực

Nếu bạn không muốn sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 theo yêu cầu, bạn có thể tạo phiên bản trình xác thực theo cách thủ công bằng cách sử dụng mặt tiền

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

98. Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

99 trên mặt tiền tạo một phiên bản trình xác thực mới

89

Đối số đầu tiên được truyền cho phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

99 là dữ liệu đang được xác thực. Đối số thứ hai là một mảng các quy tắc xác thực sẽ được áp dụng cho dữ liệu

Sau khi xác định xem xác thực yêu cầu có thất bại hay không, bạn có thể sử dụng phương pháp

801 để flash thông báo lỗi vào phiên. Khi sử dụng phương pháp này, biến 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 sẽ tự động được chia sẻ với chế độ xem của bạn sau khi chuyển hướng, cho phép bạn dễ dàng hiển thị lại cho người dùng. Phương thức
801 chấp nhận trình xác thực, 
804 hoặc PHP 
805

Dừng khi xác nhận lần đầu không thành công

Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

80 sẽ thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực

10

Chuyển hướng tự động

Nếu bạn muốn tạo một phiên bản trình xác thực theo cách thủ công nhưng vẫn tận dụng lợi thế của chuyển hướng tự động do phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 của yêu cầu HTTP cung cấp, bạn có thể gọi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 trên một phiên bản trình xác thực hiện có. Nếu xác thực không thành công, người dùng sẽ tự động được chuyển hướng hoặc trong trường hợp yêu cầu XHR, một

11

Bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

34 để lưu trữ các thông báo lỗi trong if xác thực không thành công

12

Túi lỗi được đặt tên

Nếu bạn có nhiều biểu mẫu trên một trang, bạn có thể đặt tên cho

804 chứa các lỗi xác thực, cho phép bạn truy xuất các thông báo lỗi cho một biểu mẫu cụ thể. Để đạt được điều này, hãy chuyển một tên làm đối số thứ hai cho 
801

13

Sau đó, bạn có thể truy cập phiên bản có tên

804 từ biến 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39

14

Tùy chỉnh Thông báo Lỗi

Nếu cần, bạn có thể cung cấp thông báo lỗi tùy chỉnh mà phiên bản trình xác thực nên sử dụng thay vì thông báo lỗi mặc định do Laravel cung cấp. Có một số cách để chỉ định thông báo tùy chỉnh. Trước tiên, bạn có thể chuyển các thông báo tùy chỉnh làm đối số thứ ba cho phương thức

814

15

Trong ví dụ này, trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93 sẽ được thay thế bằng tên thực của trường được xác thực. Bạn cũng có thể sử dụng các trình giữ chỗ khác trong thông báo xác thực. Ví dụ

16

Chỉ định một thông báo tùy chỉnh cho một thuộc tính nhất định

Đôi khi bạn có thể muốn chỉ định một thông báo lỗi tùy chỉnh chỉ cho một thuộc tính cụ thể. Bạn có thể làm như vậy bằng cách sử dụng ký hiệu "dấu chấm". Chỉ định tên của thuộc tính trước, sau đó là quy tắc

17

Chỉ định giá trị thuộc tính tùy chỉnh

Nhiều thông báo lỗi tích hợp của Laravel bao gồm một trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93 được thay thế bằng tên của trường hoặc thuộc tính được xác thực. Để tùy chỉnh các giá trị được sử dụng để thay thế các trình giữ chỗ này cho các trường cụ thể, bạn có thể chuyển một mảng các thuộc tính tùy chỉnh làm đối số thứ tư cho phương thức
814

18

Móc sau khi xác thực

Bạn cũng có thể đính kèm các cuộc gọi lại để chạy sau khi xác thực hoàn tất. Điều này cho phép bạn dễ dàng thực hiện xác thực thêm và thậm chí thêm nhiều thông báo lỗi hơn vào bộ sưu tập thông báo. Để bắt đầu, hãy gọi phương thức

818 trên phiên bản trình xác thực

19

Làm việc với đầu vào đã xác thực

Sau khi xác thực dữ liệu yêu cầu đến bằng yêu cầu biểu mẫu hoặc phiên bản trình xác thực được tạo thủ công, bạn có thể muốn truy xuất dữ liệu yêu cầu đến thực sự đã trải qua quá trình xác thực. Điều này có thể được thực hiện theo nhiều cách. Đầu tiên, bạn có thể gọi phương thức

819 trên một yêu cầu biểu mẫu hoặc phiên bản trình xác thực. Phương thức này trả về một mảng dữ liệu đã được xác thực

30

Ngoài ra, bạn có thể gọi phương thức

820 trên yêu cầu biểu mẫu hoặc phiên bản trình xác thực. Phương thức này trả về một thể hiện của 
821. Đối tượng này hiển thị các phương thức 
822, 
823 và 
824 để truy xuất một tập hợp con của dữ liệu đã xác thực hoặc toàn bộ mảng dữ liệu đã xác thực

31

Ngoài ra, phiên bản

821 có thể được lặp lại và truy cập giống như một mảng

32

Nếu bạn muốn thêm các trường bổ sung vào dữ liệu đã xác thực, bạn có thể gọi phương thức

826

33

Nếu bạn muốn truy xuất dữ liệu đã được xác thực dưới dạng một phiên bản bộ sưu tập, bạn có thể gọi phương thức

827

34

Làm việc với thông báo lỗi

Sau khi gọi phương thức

828 trên phiên bản 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

98, bạn sẽ nhận được phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

45, phiên bản này có nhiều phương thức thuận tiện để làm việc với các thông báo lỗi. Biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

39 được tự động cung cấp cho tất cả các chế độ xem cũng là một thể hiện của lớp
804

Truy xuất thông báo lỗi đầu tiên cho một trường

Để truy xuất thông báo lỗi đầu tiên cho một trường nhất định, hãy sử dụng phương pháp

833

35

Truy xuất tất cả các thông báo lỗi cho một trường

Nếu bạn cần truy xuất một mảng gồm tất cả các thông báo cho một trường nhất định, hãy sử dụng phương thức

834

36

Nếu bạn đang xác thực trường dạng mảng, bạn có thể truy xuất tất cả thông báo cho từng phần tử mảng bằng cách sử dụng ký tự

835

37

Truy xuất tất cả các thông báo lỗi cho tất cả các trường

Để truy xuất một mảng gồm tất cả các thông báo cho tất cả các trường, hãy sử dụng phương thức

824

38

Xác định xem có tồn tại thông báo cho một trường hay không

Phương pháp

837 có thể được sử dụng để xác định xem có bất kỳ thông báo lỗi nào tồn tại đối với một trường nhất định hay không

39

Chỉ định tin nhắn tùy chỉnh trong tệp ngôn ngữ

Mỗi quy tắc xác thực tích hợp của Laravel đều có một thông báo lỗi nằm trong tệp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

47 của ứng dụng của bạn. Trong tệp này, bạn sẽ tìm thấy mục dịch cho từng quy tắc xác thực. Bạn có thể tự do thay đổi hoặc sửa đổi các thông báo này dựa trên nhu cầu của ứng dụng của bạn

Ngoài ra, bạn có thể sao chép tệp này sang thư mục ngôn ngữ dịch thuật khác để dịch các thông báo cho ngôn ngữ của ứng dụng của bạn. Để tìm hiểu thêm về bản địa hóa Laravel, hãy xem tài liệu bản địa hóa đầy đủ

Thông báo tùy chỉnh cho các thuộc tính cụ thể

Bạn có thể tùy chỉnh các thông báo lỗi được sử dụng cho các kết hợp quy tắc và thuộc tính được chỉ định trong các tệp ngôn ngữ xác thực của ứng dụng của bạn. Để làm như vậy, hãy thêm các tùy chỉnh tin nhắn của bạn vào mảng ________ 1839 trong tệp ngôn ngữ ________ 1840 của ứng dụng của bạn

50

Chỉ định thuộc tính trong tệp ngôn ngữ

Nhiều thông báo lỗi tích hợp của Laravel bao gồm một trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93 được thay thế bằng tên của trường hoặc thuộc tính được xác thực. Nếu bạn muốn thay thế phần

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

93 của thông báo xác thực bằng một giá trị tùy chỉnh, bạn có thể chỉ định tên thuộc tính tùy chỉnh trong mảng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

95 của tệp ngôn ngữ
840 của mình

51

Chỉ định giá trị trong tệp ngôn ngữ

Một số thông báo lỗi quy tắc xác thực tích hợp của Laravel chứa trình giữ chỗ

845 được thay thế bằng giá trị hiện tại của thuộc tính yêu cầu. Tuy nhiên, đôi khi bạn có thể cần thay thế phần 
845 trong thông báo xác thực của mình bằng một biểu thị tùy chỉnh của giá trị. Ví dụ: hãy xem xét quy tắc sau đây chỉ định rằng số thẻ tín dụng là bắt buộc nếu 
847 có giá trị là 
848

52

Nếu quy tắc xác thực này không thành công, nó sẽ tạo ra thông báo lỗi sau

53

Thay vì hiển thị

848 dưới dạng giá trị loại thanh toán, bạn có thể chỉ định cách biểu thị giá trị thân thiện với người dùng hơn trong tệp ngôn ngữ 
840 của mình bằng cách xác định một mảng 
851

Sau khi xác định giá trị này, quy tắc xác thực sẽ tạo ra thông báo lỗi sau

54

Quy tắc xác thực có sẵn

Dưới đây là danh sách tất cả các quy tắc xác thực có sẵn và chức năng của chúng

Đã được chấp nhận

Trường được xác thực phải là

852, 
853, 
854 hoặc 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89. Điều này hữu ích để xác thực việc chấp nhận "Điều khoản dịch vụ" hoặc các trường tương tự

accept_if. trường khác, giá trị,

Trường đang được xác thực phải là

852, 
853, 
854 hoặc 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89 nếu một trường khác đang được xác thực bằng một giá trị đã chỉ định. Điều này hữu ích để xác thực việc chấp nhận "Điều khoản dịch vụ" hoặc các trường tương tự

active_url

Trường được xác thực phải có bản ghi A hoặc AAAA hợp lệ theo chức năng PHP

860. Tên máy chủ của URL đã cung cấp được trích xuất bằng cách sử dụng hàm PHP 
861 trước khi được chuyển đến 
860

sau đó. ngày tháng

Trường được xác thực phải là một giá trị sau một ngày nhất định. Ngày sẽ được chuyển vào hàm ________ 1863 PHP để được chuyển đổi thành phiên bản ________ 1864 hợp lệ

55

Thay vì chuyển một chuỗi ngày để được đánh giá bởi

863, bạn có thể chỉ định một trường khác để so sánh với ngày

56

after_or_equal. ngày tháng

Trường được xác thực phải là một giá trị sau hoặc bằng ngày đã cho. Để biết thêm thông tin, hãy xem quy tắc

chữ cái

Trường được xác thực phải hoàn toàn là ký tự chữ cái

alpha_dash

Trường được xác thực có thể có các ký tự chữ và số, cũng như dấu gạch ngang và dấu gạch dưới

alpha_num

Trường được xác thực phải hoàn toàn là ký tự chữ và số

mảng

Trường được xác thực phải là PHP

805

Khi các giá trị bổ sung được cung cấp cho quy tắc

805, mỗi khóa trong mảng đầu vào phải có trong danh sách các giá trị được cung cấp cho quy tắc. Trong ví dụ sau, khóa 
868 trong mảng đầu vào không hợp lệ vì nó không có trong danh sách các giá trị được cung cấp cho quy tắc 
805

57

Nói chung, bạn phải luôn chỉ định các khóa mảng được phép có trong mảng của mình

ascii

Trường được xác thực phải hoàn toàn là các ký tự ASCII 7 bit

bảo lãnh

Ngừng chạy các quy tắc xác thực cho trường sau lần xác thực đầu tiên không thành công

Mặc dù quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

35 sẽ chỉ dừng xác thực một trường cụ thể khi nó gặp lỗi xác thực, nhưng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

80 sẽ thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực.

10

trước. ngày tháng

Trường được xác thực phải là một giá trị trước ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

863 để được chuyển đổi thành một phiên bản 
864 hợp lệ. Ngoài ra, giống như quy tắc, tên của một trường khác đang được xác thực có thể được cung cấp dưới dạng giá trị của 
875

before_or_equal. ngày tháng

Trường được xác thực phải là một giá trị trước hoặc bằng ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

863 để được chuyển đổi thành một phiên bản 
864 hợp lệ. Ngoài ra, giống như quy tắc, tên của một trường khác đang được xác thực có thể được cung cấp dưới dạng giá trị của 
875

ở giữa. nhỏ nhất lớn nhất

Trường được xác thực phải có kích thước nằm trong khoảng từ tối thiểu đến tối đa đã cho (đã bao gồm). Các chuỗi, số, mảng và tệp được đánh giá theo cùng một kiểu với quy tắc

boolean

Trường được xác thực phải có thể được truyền dưới dạng boolean. Đầu vào được chấp nhận là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89,

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88,
854, 
884, 
885 và 
886

đã xác nhận

Trường được xác thực phải có trường phù hợp là

887. Ví dụ: nếu trường được xác thực là 
888, thì trường 
889 phù hợp phải có trong thông tin đầu vào

mật khẩu hiện tại

Trường được xác thực phải khớp với mật khẩu của người dùng được xác thực. Bạn có thể chỉ định một trình bảo vệ xác thực bằng cách sử dụng tham số đầu tiên của quy tắc

59

ngày tháng

Trường được xác thực phải là một ngày hợp lệ, không liên quan theo hàm PHP

863

ngày_bằng. ngày tháng

Trường được xác thực phải bằng ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

863 để được chuyển đổi thành một phiên bản 
864 hợp lệ

Định dạng ngày tháng. định dạng,

Trường được xác thực phải khớp với một trong các định dạng đã cho. Bạn nên sử dụng

875 hoặc 
894 khi xác thực một trường, không phải cả hai. Quy tắc xác thực này hỗ trợ tất cả các định dạng được hỗ trợ bởi lớp DateTime của PHP

số thập phân. nhỏ nhất lớn nhất

Trường được xác thực phải là số và phải chứa số vị trí thập phân đã chỉ định

90

suy giảm

Trường được xác thực phải là

895, 
896, 
884 hoặc 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88

bị từ chối_if. trường khác, giá trị,

Trường đang được xác thực phải là

895, 
896, 
884 hoặc 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88 nếu một trường khác đang được xác thực bằng một giá trị đã chỉ định

khác biệt. cánh đồng

Trường được xác thực phải có giá trị khác với trường

chữ số. giá trị

Số nguyên được xác thực phải có độ dài giá trị chính xác

chữ số_giữa. nhỏ nhất lớn nhất

Xác thực số nguyên phải có độ dài giữa tối thiểu và tối đa đã cho

kích thước

Tệp đang được xác thực phải là một hình ảnh đáp ứng các ràng buộc về kích thước như được chỉ định bởi các tham số của quy tắc

91

ràng buộc có sẵn là. min_width, max_width, min_height, max_height, chiều rộng, chiều cao, tỷ lệ

Một ràng buộc tỷ lệ phải được biểu diễn bằng chiều rộng chia cho chiều cao. Điều này có thể được xác định bằng một phân số như

103 hoặc một số float như 
104

92

Vì quy tắc này yêu cầu một số đối số, nên bạn có thể sử dụng phương pháp

105 để xây dựng quy tắc một cách trôi chảy

93

riêng biệt

Khi xác thực mảng, trường được xác thực không được có bất kỳ giá trị trùng lặp nào

Theo mặc định, Distinct sử dụng phép so sánh biến lỏng lẻo. Để sử dụng phép so sánh nghiêm ngặt, bạn có thể thêm tham số

106 vào định nghĩa quy tắc xác thực của mình

94

Bạn có thể thêm

107 vào các đối số của quy tắc xác thực để làm cho quy tắc bỏ qua sự khác biệt về cách viết hoa

95

không_bắt_đầu_với. thực phẩm, thanh,

Trường được xác thực không được bắt đầu bằng một trong các giá trị đã cho

không_cuối_với. thực phẩm, thanh,

Trường được xác thực không được kết thúc bằng một trong các giá trị đã cho

e-mail

Trường được xác thực phải được định dạng dưới dạng địa chỉ email. Quy tắc xác thực này sử dụng gói

108 để xác thực địa chỉ email. Theo mặc định, trình xác thực 
109 được áp dụng nhưng bạn cũng có thể áp dụng các kiểu xác thực khác

Ví dụ trên sẽ áp dụng xác thực

109 và 
111. Dưới đây là danh sách đầy đủ các kiểu xác thực mà bạn có thể áp dụng

  • 112. 
    109
  • 106. 
    115
  • 116. 
    111
  • 118. 
    119
  • 120. 
    121
  • 122. 
    123

Trình xác thực

120, sử dụng hàm 
125 của PHP, đi kèm với Laravel và là hành vi xác thực email mặc định của Laravel trước phiên bản 5 của Laravel. 8

Cảnh báo
Trình xác thực

116 và 
118 yêu cầu tiện ích mở rộng PHP 
128. 

kết_với. thực phẩm, thanh,

Trường được xác thực phải kết thúc bằng một trong các giá trị đã cho

liệt kê

Quy tắc

129 là quy tắc dựa trên lớp xác thực xem trường được xác thực có chứa giá trị enum hợp lệ hay không. Quy tắc 
129 chấp nhận tên của enum làm đối số hàm tạo duy nhất của nó

96

Cảnh báo
Enums chỉ khả dụng trên PHP 8. 1+.

loại trừ

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819

loại trừ_if. trường khác, giá trị

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819 nếu trường otherfield bằng giá trị

Nếu logic loại trừ có điều kiện phức tạp được yêu cầu, bạn có thể sử dụng phương pháp

135. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi được cung cấp một bao đóng, bao đóng phải trả về 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88 để cho biết liệu trường đang được xác thực có nên bị loại trừ hay không

97

loại trừ_unless. trường khác, giá trị

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819 trừ khi trường của trường khác bằng giá trị. Nếu giá trị là 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 (
141), trường được xác thực sẽ bị loại trừ trừ khi trường so sánh là 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 hoặc trường so sánh bị thiếu trong dữ liệu yêu cầu

loại trừ_với. lĩnh vực khác

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819 nếu có trường otherfield

loại trừ_không. lĩnh vực khác

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819 nếu trường otherfield không có mặt

tồn tại. bảng, cột

Trường được xác thực phải tồn tại trong một bảng cơ sở dữ liệu nhất định

Cách sử dụng cơ bản của quy tắc tồn tại

Nếu tùy chọn

147 không được chỉ định, tên trường sẽ được sử dụng. Vì vậy, trong trường hợp này, quy tắc sẽ xác thực rằng bảng cơ sở dữ liệu 
148 chứa bản ghi có giá trị cột 
149 khớp với giá trị thuộc tính 
149 của yêu cầu

Chỉ định tên cột tùy chỉnh

Bạn có thể chỉ định rõ ràng tên cột cơ sở dữ liệu sẽ được sử dụng theo quy tắc xác thực bằng cách đặt nó sau tên bảng cơ sở dữ liệu

98

Đôi khi, bạn có thể cần chỉ định một kết nối cơ sở dữ liệu cụ thể sẽ được sử dụng cho truy vấn

151. Bạn có thể thực hiện việc này bằng cách thêm tên kết nối vào tên bảng

99

Thay vì chỉ định trực tiếp tên bảng, bạn có thể chỉ định mô hình Eloquent sẽ được sử dụng để xác định tên bảng

00

Nếu bạn muốn tùy chỉnh truy vấn được thực thi theo quy tắc xác thực, bạn có thể sử dụng lớp

152 để xác định quy tắc một cách trôi chảy. Trong ví dụ này, chúng tôi cũng sẽ chỉ định các quy tắc xác thực dưới dạng một mảng thay vì sử dụng ký tự 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33 để phân định chúng

01

Bạn có thể chỉ định rõ ràng tên cột cơ sở dữ liệu sẽ được sử dụng bởi quy tắc

151 được tạo bởi phương thức 
155 bằng cách cung cấp tên cột làm đối số thứ hai cho phương thức 
151

02

tập tin

Trường được xác thực phải là tệp đã tải lên thành công

điền

Trường được xác thực không được để trống khi nó hiện diện

gt. cánh đồng

Trường được xác thực phải lớn hơn trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

gte. cánh đồng

Trường được xác thực phải lớn hơn hoặc bằng trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

hình ảnh

Tệp được xác thực phải là hình ảnh (jpg, jpeg, png, bmp, gif, svg hoặc webp)

Trong. thực phẩm, thanh,

Trường được xác thực phải được bao gồm trong danh sách giá trị đã cho. Vì quy tắc này thường yêu cầu bạn phải

159 một mảng, phương thức 
160 có thể được sử dụng để xây dựng quy tắc một cách trôi chảy

03

Khi quy tắc

161 được kết hợp với quy tắc 
805, mỗi giá trị trong mảng đầu vào phải có mặt trong danh sách các giá trị được cung cấp cho quy tắc 
161. Trong ví dụ sau, mã sân bay 
164 trong mảng đầu vào không hợp lệ vì nó không có trong danh sách sân bay được cung cấp cho quy tắc 
161

04

in_array. lĩnh vực khác. *

Trường được xác thực phải tồn tại trong các giá trị của trường khác

số nguyên

Trường được xác thực phải là số nguyên

Cảnh báo
Quy tắc xác thực này không xác minh rằng đầu vào thuộc loại biến "số nguyên", chỉ xác minh rằng đầu vào thuộc loại được chấp nhận bởi quy tắc

166 của PHP. Nếu bạn cần xác thực đầu vào là một số, vui lòng sử dụng quy tắc này kết hợp với. 

ip

Trường được xác thực phải là một địa chỉ IP

ipv4

Trường được xác thực phải là địa chỉ IPv4

ipv6

Trường được xác thực phải là địa chỉ IPv6

json

Trường được xác thực phải là một chuỗi JSON hợp lệ

trung úy. cánh đồng

Trường được xác thực phải nhỏ hơn trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

lte. cánh đồng

Trường được xác thực phải nhỏ hơn hoặc bằng trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

chữ thường

Trường được xác thực phải là chữ thường

địa chỉ MAC

Trường được xác thực phải là địa chỉ MAC

tối đa. giá trị

Trường được xác thực phải nhỏ hơn hoặc bằng giá trị tối đa. Các chuỗi, số, mảng và tệp được đánh giá theo cùng một kiểu với quy tắc

max_digits. giá trị

Số nguyên được xác thực phải có độ dài giá trị tối đa

kịch câm. văn bản/đồng bằng,

Tệp được xác thực phải khớp với một trong các loại MIME đã cho

05

Để xác định loại MIME của tệp đã tải lên, nội dung của tệp sẽ được đọc và khung sẽ cố gắng đoán loại MIME, loại này có thể khác với loại MIME do khách hàng cung cấp

kịch câm. thực phẩm, thanh,

Tệp đang được xác thực phải có loại MIME tương ứng với một trong các tiện ích mở rộng được liệt kê

Cách sử dụng cơ bản của quy tắc MIME

06

Mặc dù bạn chỉ cần chỉ định phần mở rộng, quy tắc này thực sự xác thực loại MIME của tệp bằng cách đọc nội dung của tệp và đoán loại MIME của nó. Có thể tìm thấy danh sách đầy đủ các loại MIME và các tiện ích mở rộng tương ứng của chúng tại vị trí sau

https. //svn. apache. org/repos/asf/httpd/httpd/trunk/docs/conf/mime. các loại

tối thiểu. giá trị

Trường được xác thực phải có giá trị tối thiểu. Các chuỗi, số, mảng và tệp được đánh giá theo cùng một kiểu với quy tắc

min_digits. giá trị

Số nguyên được xác thực phải có độ dài giá trị tối thiểu

multiple_of. giá trị

Trường được xác thực phải là bội số của giá trị

Cảnh báo
Cần có phần mở rộng PHP

172 để sử dụng quy tắc 
173. 

not_in. thực phẩm, thanh,

Trường được xác thực không được bao gồm trong danh sách giá trị đã cho. Phương pháp

174 có thể được sử dụng để xây dựng thành thạo quy tắc

07

not_regex. mẫu

Trường được xác thực không được khớp với biểu thức chính quy đã cho

Trong nội bộ, quy tắc này sử dụng hàm PHP

175. Mẫu được chỉ định phải tuân theo cùng định dạng theo yêu cầu của 
175 và do đó cũng bao gồm các dấu phân cách hợp lệ. Ví dụ. 
177

Cảnh báo
Khi sử dụng các mẫu

178 / 
179, có thể cần chỉ định quy tắc xác thực của bạn bằng cách sử dụng một mảng thay vì sử dụng dấu phân cách 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33, đặc biệt nếu biểu thức chính quy chứa ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33.

vô giá trị

Trường được xác thực có thể là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59

con số

Trường được xác thực phải là số

mật khẩu mở khóa

Trường được xác thực phải khớp với mật khẩu của người dùng được xác thực

Cảnh báo
Quy tắc này đã được đổi tên thành

183 với ý định xóa nó trong Laravel 9. Vui lòng sử dụng quy tắc thay thế. 

hiện nay

Trường được xác thực phải có trong dữ liệu đầu vào nhưng có thể để trống

Cấm

Trường được xác thực phải là một chuỗi trống hoặc không có

bị cấm_if. trường khác, giá trị,

Trường được xác thực phải là một chuỗi trống hoặc không có nếu trường otherfield bằng bất kỳ giá trị nào

Nếu logic cấm có điều kiện phức tạp được yêu cầu, bạn có thể sử dụng phương pháp

184. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi được cung cấp một bao đóng, bao đóng phải trả về 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88 để cho biết liệu trường đang được xác thực có nên bị cấm hay không

08

bị cấm_unless. trường khác, giá trị,

Trường được xác thực phải là một chuỗi trống hoặc không có trừ khi trường otherfield bằng bất kỳ giá trị nào

cấm. lĩnh vực khác,

Nếu có trường đang xác thực, thì không có trường nào trong trường khác có thể có, ngay cả khi trống

biểu thức chính quy. mẫu

Trường được xác thực phải khớp với biểu thức chính quy đã cho

Trong nội bộ, quy tắc này sử dụng hàm PHP

175. Mẫu được chỉ định phải tuân theo cùng định dạng theo yêu cầu của 
175 và do đó cũng bao gồm các dấu phân cách hợp lệ. Ví dụ. 
189

Cảnh báo
Khi sử dụng các mẫu

178 / 
179, có thể cần chỉ định các quy tắc trong một mảng thay vì sử dụng các dấu phân cách 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33, đặc biệt nếu biểu thức chính quy chứa ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33.

cần thiết

Trường được xác thực phải có trong dữ liệu đầu vào và không trống. Một trường được coi là "trống" nếu một trong các điều kiện sau là đúng

  • Giá trị là

    namespace App\Http\Controllers;

    use App\Http\Controllers\Controller;

    use Illuminate\Http\Request;

    class PostController extends Controller

    * Show the form to create a new blog post.

    * @return \Illuminate\View\View

    return view('post.create');

    * @param \Illuminate\Http\Request $request

    * @return \Illuminate\Http\Response

    public function store(Request $request)

    // Validate and store the blog post...

    59
  • Giá trị là một chuỗi rỗng
  • Giá trị là một mảng trống hoặc đối tượng
    195 trống
  • Giá trị là một tệp đã tải lên không có đường dẫn

bắt buộc_if. trường khác, giá trị,

Trường được xác thực phải có mặt và không trống nếu trường otherfield bằng bất kỳ giá trị nào

Nếu bạn muốn xây dựng một điều kiện phức tạp hơn cho quy tắc

196, bạn có thể sử dụng phương pháp 
197. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi thông qua một lần đóng, lần đóng sẽ trả về 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88 để cho biết trường được xác thực có bắt buộc không

09

bắt buộc_unless. trường khác, giá trị,

Trường được xác thực phải có mặt và không trống trừ khi trường otherfield bằng bất kỳ giá trị nào. Điều này cũng có nghĩa là trường khác phải có trong dữ liệu yêu cầu trừ khi giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59. Nếu giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 (
302), trường được xác thực sẽ được yêu cầu trừ khi trường so sánh là 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59 hoặc trường so sánh bị thiếu trong dữ liệu yêu cầu

bắt buộc_với. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi có bất kỳ trường nào được chỉ định khác có mặt và không trống

required_with_all. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi tất cả các trường được chỉ định khác có mặt và không trống

bắt buộc_không. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi bất kỳ trường nào được chỉ định khác trống hoặc không có

bắt buộc_không_có_tất_cả. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi tất cả các trường được chỉ định khác trống hoặc không có

required_array_keys. thực phẩm, thanh,

Trường được xác thực phải là một mảng và phải chứa ít nhất các khóa đã chỉ định

như nhau. cánh đồng

Trường đã cho phải khớp với trường đang được xác thực

kích thước. giá trị

Trường được xác thực phải có kích thước khớp với giá trị đã cho. Đối với dữ liệu chuỗi, giá trị tương ứng với số ký tự. Đối với dữ liệu số, giá trị tương ứng với một giá trị số nguyên nhất định (thuộc tính cũng phải có quy tắc

167 hoặc 
305). Đối với một mảng, kích thước tương ứng với 
306 của mảng. Đối với tệp, kích thước tương ứng với kích thước tệp tính bằng kilobyte. Hãy xem xét một số ví dụ

90

bắt đầu với. thực phẩm, thanh,

Trường được xác thực phải bắt đầu bằng một trong các giá trị đã cho

chuỗi

Trường được xác thực phải là một chuỗi. Nếu bạn muốn cho phép trường cũng là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

59, bạn nên gán quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

63 cho trường

Múi giờ

Trường được xác thực phải là mã định danh múi giờ hợp lệ theo hàm PHP

309

duy nhất. bảng, cột

Trường được xác thực không được tồn tại trong bảng cơ sở dữ liệu đã cho

Chỉ định tên bảng / cột tùy chỉnh

Thay vì chỉ định trực tiếp tên bảng, bạn có thể chỉ định mô hình Eloquent sẽ được sử dụng để xác định tên bảng

91

Tùy chọn

147 có thể được sử dụng để chỉ định cột cơ sở dữ liệu tương ứng của trường. Nếu tùy chọn 
147 không được chỉ định, tên của trường được xác thực sẽ được sử dụng

92

Chỉ định kết nối cơ sở dữ liệu tùy chỉnh

Đôi khi, bạn có thể cần đặt kết nối tùy chỉnh cho các truy vấn cơ sở dữ liệu do Trình xác thực thực hiện. Để thực hiện điều này, bạn có thể thêm tên kết nối vào tên bảng

93

Buộc một quy tắc duy nhất bỏ qua một ID đã cho

Đôi khi, bạn có thể muốn bỏ qua một ID nhất định trong quá trình xác thực duy nhất. Ví dụ: xem xét màn hình "cập nhật hồ sơ" bao gồm tên, địa chỉ email và vị trí của người dùng. Bạn có thể sẽ muốn xác minh rằng địa chỉ email là duy nhất. Tuy nhiên, nếu người dùng chỉ thay đổi trường tên chứ không phải trường email, bạn không muốn xảy ra lỗi xác thực vì người dùng đã là chủ sở hữu của địa chỉ email được đề cập

Để hướng dẫn trình xác thực bỏ qua ID của người dùng, chúng tôi sẽ sử dụng lớp

152 để xác định quy tắc một cách trôi chảy. Trong ví dụ này, chúng tôi cũng sẽ chỉ định các quy tắc xác thực dưới dạng một mảng thay vì sử dụng ký tự 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

33 để phân định các quy tắc

94

Cảnh báo
Bạn không bao giờ được chuyển bất kỳ đầu vào yêu cầu do người dùng kiểm soát nào vào phương thức

314. Thay vào đó, bạn chỉ nên chuyển một ID duy nhất do hệ thống tạo, chẳng hạn như ID tăng tự động hoặc UUID từ một phiên bản mô hình Eloquent. Nếu không, ứng dụng của bạn sẽ dễ bị tấn công SQL injection. 

Thay vì chuyển giá trị của khóa mô hình sang phương thức

314, bạn cũng có thể chuyển toàn bộ phiên bản mô hình. Laravel sẽ tự động trích xuất khóa từ mô hình

95

Nếu bảng của bạn sử dụng tên cột khóa chính khác với

316, bạn có thể chỉ định tên của cột khi gọi phương thức 
314

96

Theo mặc định, quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

36 sẽ kiểm tra tính duy nhất của cột khớp với tên của thuộc tính đang được xác thực. Tuy nhiên, bạn có thể chuyển một tên cột khác làm đối số thứ hai cho phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

36

97

Thêm các mệnh đề bổ sung

Bạn có thể chỉ định các điều kiện truy vấn bổ sung bằng cách tùy chỉnh truy vấn bằng phương pháp

320. Ví dụ: hãy thêm một điều kiện truy vấn để phạm vi truy vấn chỉ tìm kiếm các bản ghi có giá trị cột 
321 là 
854

98

chữ hoa

Trường được xác thực phải là chữ hoa

url

Trường được xác thực phải là một URL hợp lệ

ulid

Trường được xác thực phải là Mã định danh có thể sắp xếp theo từ điển duy nhất toàn cầu (ULID) hợp lệ

uuid

Trường được xác thực phải là số nhận dạng duy nhất toàn cầu RFC 4122 (phiên bản 1, 3, 4 hoặc 5) hợp lệ (UUID)

Thêm quy tắc có điều kiện

Bỏ qua xác thực khi các trường có giá trị nhất định

Đôi khi, bạn có thể không muốn xác thực một trường nhất định nếu một trường khác có một giá trị nhất định. Bạn có thể thực hiện điều này bằng cách sử dụng quy tắc xác thực

323. Trong ví dụ này, các trường 
324 và 
325 sẽ không được xác thực nếu trường 
326 có giá trị là 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

88

99

Ngoài ra, bạn có thể sử dụng quy tắc

328 để không xác thực một trường nhất định trừ khi một trường khác có giá trị nhất định

count();

    dd($employee);
  }
}
60

Xác thực khi có mặt

Trong một số trường hợp, bạn có thể muốn chạy kiểm tra xác thực đối với một trường chỉ khi trường đó có trong dữ liệu đang được xác thực. Để nhanh chóng hoàn thành việc này, hãy thêm quy tắc

329 vào danh sách quy tắc của bạn

count();

    dd($employee);
  }
}
61

Trong ví dụ trên, trường

330 sẽ chỉ được xác thực nếu nó có trong mảng 
331

Lưu ý
Nếu bạn đang cố xác thực một trường phải luôn hiện diện nhưng có thể trống, hãy kiểm tra.

Xác thực có điều kiện phức tạp

Đôi khi bạn có thể muốn thêm các quy tắc xác thực dựa trên logic điều kiện phức tạp hơn. Ví dụ: bạn có thể chỉ muốn yêu cầu một trường nhất định nếu trường khác có giá trị lớn hơn 100. Hoặc, bạn có thể cần hai trường để chỉ có một giá trị nhất định khi có trường khác. Thêm các quy tắc xác thực này không phải là một điều khó khăn. Đầu tiên, tạo một phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

98 với các quy tắc tĩnh không bao giờ thay đổi của bạn

count();

    dd($employee);
  }
}
62

Giả sử ứng dụng web của chúng tôi dành cho người sưu tầm trò chơi. Nếu một nhà sưu tập trò chơi đăng ký với ứng dụng của chúng tôi và họ sở hữu hơn 100 trò chơi, chúng tôi muốn họ giải thích lý do tại sao họ sở hữu nhiều trò chơi như vậy. Ví dụ: có lẽ họ điều hành một cửa hàng bán lại trò chơi hoặc có thể họ chỉ thích sưu tập trò chơi. Để thêm yêu cầu này một cách có điều kiện, chúng ta có thể sử dụng phương thức

329 trên trường hợp 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

98

count();

    dd($employee);
  }
}
63

Đối số đầu tiên được truyền cho phương thức

329 là tên của trường chúng tôi đang xác thực có điều kiện. Đối số thứ hai là danh sách các quy tắc chúng tôi muốn thêm. Nếu lệnh đóng được thông qua khi đối số thứ ba trả về 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

89, các quy tắc sẽ được thêm vào. Phương pháp này giúp dễ dàng xây dựng các xác thực có điều kiện phức tạp. Bạn thậm chí có thể thêm xác thực có điều kiện cho một số trường cùng một lúc

count();

    dd($employee);
  }
}
64

Lưu ý
Tham số

337 được truyền cho lần đóng của bạn sẽ là một phiên bản của 
338 và có thể được sử dụng để truy cập thông tin đầu vào và tệp của bạn đang được xác thực. 

Xác thực mảng có điều kiện phức tạp

Đôi khi, bạn có thể muốn xác thực một trường dựa trên một trường khác trong cùng một mảng lồng nhau có chỉ mục mà bạn không biết. Trong những tình huống này, bạn có thể cho phép bao đóng của mình nhận đối số thứ hai sẽ là mục riêng lẻ hiện tại trong mảng đang được xác thực

count();

    dd($employee);
  }
}
65

Giống như tham số

337 được truyền cho bao đóng, tham số 
340 là một thể hiện của 
338 khi dữ liệu thuộc tính là một mảng; 

Xác thực mảng

Như đã thảo luận trong phần , quy tắc

805 chấp nhận danh sách các khóa mảng được phép. Nếu có bất kỳ khóa bổ sung nào trong mảng, quá trình xác thực sẽ không thành công

count();

    dd($employee);
  }
}
66

Nói chung, bạn phải luôn chỉ định các khóa mảng được phép có trong mảng của mình. Mặt khác, các phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

21 và
819 của trình xác thực sẽ trả về tất cả dữ liệu đã xác thực, bao gồm mảng và tất cả các khóa của nó, ngay cả khi các khóa đó không được xác thực bởi các quy tắc xác thực mảng lồng nhau khác

Xác thực đầu vào mảng lồng nhau

Việc xác thực các trường nhập biểu mẫu dựa trên mảng lồng nhau không phải là một điều khó khăn. Bạn có thể sử dụng "ký hiệu dấu chấm" để xác thực các thuộc tính trong một mảng. Ví dụ: nếu yêu cầu HTTP đến chứa trường

346, bạn có thể xác thực nó như vậy

count();

    dd($employee);
  }
}
67

Bạn cũng có thể xác thực từng phần tử của một mảng. Ví dụ: để xác thực rằng mỗi email trong trường nhập mảng nhất định là duy nhất, bạn có thể thực hiện như sau

count();

    dd($employee);
  }
}
68

Tương tự như vậy, bạn có thể sử dụng ký tự

835 khi chỉ định , giúp dễ dàng sử dụng một thông báo xác thực duy nhất cho các trường dựa trên mảng

count();

    dd($employee);
  }
}
69

Truy cập dữ liệu mảng lồng nhau

Đôi khi bạn có thể cần truy cập giá trị cho phần tử mảng lồng nhau đã cho khi gán quy tắc xác thực cho thuộc tính. Bạn có thể thực hiện việc này bằng cách sử dụng phương pháp

348. Phương thức 
349 chấp nhận một bao đóng sẽ được gọi cho mỗi lần lặp của thuộc tính mảng được xác thực và sẽ nhận được giá trị của thuộc tính và tên thuộc tính rõ ràng, được mở rộng đầy đủ. Việc đóng sẽ trả về một mảng các quy tắc để gán cho phần tử mảng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

00

Thông báo Lỗi Chỉ mục & Vị trí

Khi xác thực mảng, bạn có thể muốn tham chiếu chỉ mục hoặc vị trí của một mục cụ thể không xác thực được trong thông báo lỗi được hiển thị bởi ứng dụng của bạn. Để thực hiện điều này, bạn có thể bao gồm các trình giữ chỗ

350 và 
351 trong

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

01

Với ví dụ trên, quá trình xác thực sẽ không thành công và người dùng sẽ gặp lỗi sau "Vui lòng mô tả ảnh #2. "

Xác thực tập tin

Laravel cung cấp nhiều quy tắc xác thực có thể được sử dụng để xác thực các tệp đã tải lên, chẳng hạn như

352, 
353, 
354 và 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

38. Mặc dù bạn có thể tự do chỉ định các quy tắc này riêng lẻ khi xác thực tệp, Laravel cũng cung cấp trình tạo quy tắc xác thực tệp thông thạo mà bạn có thể thấy thuận tiện.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

02

Nếu ứng dụng của bạn chấp nhận hình ảnh do người dùng của bạn tải lên, bạn có thể sử dụng phương thức xây dựng

353 của quy tắc 
356 để chỉ ra rằng tệp đã tải lên phải là hình ảnh. Ngoài ra, quy tắc 
358 có thể được sử dụng để giới hạn kích thước của hình ảnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

03

Lưu ý
Bạn có thể tìm thêm thông tin về việc xác thực kích thước hình ảnh trong.

Loại tập tin

Mặc dù bạn chỉ cần chỉ định phần mở rộng khi gọi phương thức

359, phương thức này thực sự xác thực loại MIME của tệp bằng cách đọc nội dung của tệp và đoán loại MIME của nó. Có thể tìm thấy danh sách đầy đủ các loại MIME và các tiện ích mở rộng tương ứng của chúng tại vị trí sau

https. //svn. apache. org/repos/asf/httpd/httpd/trunk/docs/conf/mime. các loại

Xác thực mật khẩu

Để đảm bảo rằng mật khẩu có mức độ phức tạp phù hợp, bạn có thể sử dụng đối tượng quy tắc

360 của Laravel

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

04

Đối tượng quy tắc

360 cho phép bạn dễ dàng tùy chỉnh các yêu cầu về độ phức tạp của mật khẩu cho ứng dụng của mình, chẳng hạn như chỉ định rằng mật khẩu yêu cầu ít nhất một chữ cái, số, ký hiệu hoặc ký tự có viết hoa hỗn hợp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

05

Ngoài ra, bạn có thể đảm bảo rằng mật khẩu không bị xâm phạm trong vụ rò rỉ dữ liệu mật khẩu công khai bằng phương pháp

362

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

06

Bên trong, đối tượng quy tắc

360 sử dụng mô hình k-Anonymity để xác định xem mật khẩu có bị rò rỉ thông qua haveibeenpwned hay không. com mà không làm mất quyền riêng tư hoặc bảo mật của người dùng

Theo mặc định, nếu mật khẩu xuất hiện ít nhất một lần trong một lần rò rỉ dữ liệu, mật khẩu đó sẽ bị coi là bị xâm phạm. Bạn có thể tùy chỉnh ngưỡng này bằng đối số đầu tiên của phương thức

362

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

07

Tất nhiên, bạn có thể xâu chuỗi tất cả các phương thức trong các ví dụ trên

Xác định quy tắc mật khẩu mặc định

Bạn có thể thấy thuận tiện khi chỉ định các quy tắc xác thực mặc định cho mật khẩu ở một vị trí duy nhất trong ứng dụng của mình. Bạn có thể dễ dàng thực hiện việc này bằng cách sử dụng phương thức

365, phương thức này chấp nhận một bao đóng. Việc đóng cho phương thức 
366 sẽ trả về cấu hình mặc định của quy tắc Mật khẩu. Thông thường, quy tắc ________ 2366 nên được gọi trong phương thức ________ 2368 của một trong những nhà cung cấp dịch vụ ứng dụng của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

08

Sau đó, khi bạn muốn áp dụng các quy tắc mặc định cho một mật khẩu cụ thể đang được xác thực, bạn có thể gọi phương thức

366 mà không có đối số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

09

Đôi khi, bạn có thể muốn đính kèm các quy tắc xác thực bổ sung vào quy tắc xác thực mật khẩu mặc định của mình. Bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

75 để thực hiện điều này

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

10

Quy tắc xác thực tùy chỉnh

Sử dụng đối tượng quy tắc

Laravel cung cấp nhiều quy tắc xác thực hữu ích; . Một phương pháp đăng ký quy tắc xác thực tùy chỉnh là sử dụng các đối tượng quy tắc. Để tạo một đối tượng quy tắc mới, bạn có thể sử dụng lệnh Artisan

371. Hãy sử dụng lệnh này để tạo quy tắc xác minh một chuỗi là chữ hoa. Laravel sẽ đặt quy tắc mới trong thư mục 
372. Nếu thư mục này không tồn tại, Laravel sẽ tạo nó khi bạn thực thi lệnh Artisan để tạo quy tắc của mình

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

11

Khi quy tắc đã được tạo, chúng tôi sẵn sàng xác định hành vi của nó. Một đối tượng quy tắc chứa một phương thức duy nhất.

373. Phương thức này nhận tên thuộc tính, giá trị của nó và một cuộc gọi lại sẽ được gọi khi không thành công với thông báo lỗi xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

12

Khi quy tắc đã được xác định, bạn có thể đính kèm nó vào trình xác thực bằng cách chuyển một phiên bản của đối tượng quy tắc với các quy tắc xác thực khác của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

13

Dịch thông báo xác thực

Thay vì cung cấp thông báo lỗi theo nghĩa đen cho phần đóng

374, bạn cũng có thể cung cấp khóa chuỗi dịch và hướng dẫn Laravel dịch thông báo lỗi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

14

Nếu cần, bạn có thể cung cấp các thay thế trình giữ chỗ và ngôn ngữ ưa thích làm đối số thứ nhất và thứ hai cho phương thức

375

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

15

Truy cập dữ liệu bổ sung

Nếu lớp quy tắc xác thực tùy chỉnh của bạn cần truy cập vào tất cả dữ liệu khác đang được xác thực, thì lớp quy tắc của bạn có thể triển khai giao diện

376. Giao diện này yêu cầu lớp của bạn xác định phương thức 
377. Phương thức này sẽ tự động được gọi bởi Laravel (trước khi tiến hành xác thực) với tất cả dữ liệu đang được xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

16

Hoặc, nếu quy tắc xác thực của bạn yêu cầu quyền truy cập vào phiên bản trình xác thực đang thực hiện xác thực, thì bạn có thể triển khai giao diện

378

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

17

Sử dụng Closures

Nếu bạn chỉ cần chức năng của quy tắc tùy chỉnh một lần trong ứng dụng của mình, thì bạn có thể sử dụng bao đóng thay vì đối tượng quy tắc. Việc đóng nhận tên của thuộc tính, giá trị của thuộc tính và gọi lại

374 sẽ được gọi nếu xác thực không thành công

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

18

Quy tắc ngầm

Theo mặc định, khi một thuộc tính đang được xác thực không có hoặc chứa một chuỗi trống, thì các quy tắc xác thực thông thường, bao gồm các quy tắc tùy chỉnh, sẽ không được chạy. Ví dụ: quy tắc sẽ không được chạy đối với một chuỗi trống

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view('post.create');

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store(Request $request)

// Validate and store the blog post...

19

Để quy tắc tùy chỉnh chạy ngay cả khi thuộc tính trống, quy tắc phải ngụ ý rằng thuộc tính là bắt buộc. Để tạo nhanh một đối tượng quy tắc ẩn mới, bạn có thể sử dụng lệnh Artisan

371 với tùy chọn 
382

Làm cách nào để có được bản ghi giá trị tối thiểu trong SQL?

Để tìm giá trị nhỏ nhất của một cột, hãy sử dụng hàm tổng hợp MIN(); . Nếu bạn chưa chỉ định bất kỳ cột nào khác trong mệnh đề SELECT, giá trị tối thiểu sẽ được tính cho tất cả các bản ghi trong bảng

Làm cách nào để lấy giá trị cột cụ thể trong Laravel?

Trình tạo truy vấn có nhanh hơn Eloquent không?

Tôi đã thực hiện một số kiểm tra hiệu suất giữa trình tạo truy vấn mặt tiền DB của Laravel và ORM Eloquent của Laravel. Mặt tiền DB nhanh hơn nhiều so với Eloquent đối với nhiều câu lệnh SQL (CHỌN, CẬP NHẬT, XÓA, CHÈN).

Làm cách nào để CHỌN id tối đa trong Laravel?

Bạn sẽ có thể thực hiện lựa chọn trên bảng đơn đặt hàng, sử dụng WHERE thô để tìm max( id ) trong truy vấn phụ, như thế này. \DB. table('orders')->where('id', \DB. raw("(chọn tối đa(`id`) từ đơn đặt hàng)"))->get();