我想知道如何从几列中验证独特的组合?
例如:
public class EmergencyContact extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
private ArrayList<ContactPojo> contactPojolist = new ArrayList<>();
private RecyclerView emerg_recycler;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_contact);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
emerg_recycler = (RecyclerView) findViewById(R.id.emerg_contact_recycler);
mLayoutManager = new LinearLayoutManager(this);
emerg_recycler.setLayoutManager(mLayoutManager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
ContactPojo contactPojo = new ContactPojo();
contactPojo.setName(name);
contactPojo.setNumber(number);
contactPojolist.add(contactPojo);
RentalAdapter emergencyContact = new RentalAdapter(EmergencyContact.this, contactPojolist);
emerg_recycler.setAdapter(emergencyContact);
}
}
}
}
如果有来自伦敦的数据库John Smith,那么我想给John Smith添加记录,但不能从伦敦添加记录。
我想要从字段组合的唯一键:FirstName,LastName,Address。
请帮助。
答案 0 :(得分:1)
运行命令php artisan make:rule UniqueNameAddress
。这将在app / Rules文件夹中创建一个具有给定名称(UniqueNameAddress
)
在UniqueNameAddress
课程中,有passes
方法。这是您验证所请求的名称和地址尚未被其他用户使用的位置。您可以在passes
方法中粘贴和自定义以下代码。
$matches = PhoneBook::whereFirstName(request(first_name))
->whereLastName(request(last_name))
->whereAddress(request(address))
->count();
return $matches === 0;
在同一个类的messages
方法中,设置在出现错误时将返回的消息。
使用自定义规则,如下所示
$request->validate([
'first_name' => ['required', new UniqueNameAddress],
]);