简体   繁体   English

laravel使用findOrFail来验证其他记录不是id

[英]laravel using findOrFail for validate other record not id

I have table contain id, customer_id,..etc so findOrFail will work only for id so want to check customer_id record not id. 我的表包含id,customer_id,.. etc,因此findOrFail仅适用于id,因此要检查customer_id记录而不是id。 How can I check other record using findOrFail something like this 如何使用findOrFail检查其他记录

$post = Customer::findOrFail('customer_id',$request->customer_id)

Maybe you are looking for firstOrFail() , so you can do: 也许您正在寻找firstOrFail() ,所以您可以执行以下操作:

Customer::whereCustomerId($request->customer_id)->firstOrFail();

findOrFail() only works for the primary key id so you have to use firstOrFail . findOrFail()仅适用于主键id因此您必须使用firstOrFail

Note that you could also write it as: 请注意,您也可以将其编写为:

Customer::where('customer_id', $request->customer_id)->firstOrFail();

You can use any column of a table 您可以使用表的任何列

Customer::where('<any column>', <column value>)->firstOrFail(); 

Laravel example Laravel示例

$model = App\Flight::findOrFail(1);

$model = App\Flight::where('legs', '>', 100)->firstOrFail();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM