[英]How to limit the data entry to database in Laravel 5.7
How could I limit the data that I am writing to the database. 如何限制正在写入数据库的数据。
I am trying to map Bank details to Districts. 我正在尝试将银行详细信息映射到地区。 I would like to limit the entry of Bank details to 1 District.
我想将银行详细信息的输入限制为1个区域。
To be precise, user should only be able to create a bank name for a district. 确切地说,用户应该只能为一个地区创建一个银行名称。 When they try to create a new record they would be treated with error stating that only One record should be created.
当他们尝试创建新记录时,将被错误处理,指出仅应创建一个记录。
Database Tables: 数据库表:
District
| id
| district_name
Bank
| id
| bank_name
| district_id
I have thought of showing the data of the districts that not in the bank table to a select menu. 我曾想过将不在银行表中的地区数据显示给选择菜单。
How would I limit that in eloquent relationship or are there any other options, 我该如何在雄辩的关系中限制这一点,或者还有其他选择,
Another option is to limit the result to the latest creates bank Id. 另一个选择是将结果限制为最新创建的库ID。 But I dont think this is a good idea to consider.
但是我不认为这是一个好主意。
What I tried, 我尝试过的
Bank Controller, 银行管理员
use Bank; 使用银行;
public function create()
{
$districts = Districts::all();
$banks = Bank::all();
foreach($banks->districts as $bank)
$banks1 = $bank->district_name;
return($bank1);
// return view('bank-create',compact('districts'));
}
The above code is not working, I just tried and I was not able to get through it. 上面的代码不起作用,我只是尝试了一下,但无法通过它。
I was expecting the result of the query to pass in to below select, 我期望查询的结果传递到select下面,
<select name="district_option1" class="form-control ml-5" id="">
@foreach ($bank1 as $banks1)
<option value="{{$banks1->id}}">{{$banks1->district_name}} </option>
@endforeach
</select>
My Model: 我的模特:
Bank 银行
public function districts(){
return $this->belongsTo('App\Districts','district_id','id');
}
District : 地区:
public function bank(){
return $this->hasOne('App\Bank','id','district_id'); }
You may use unique
validation for this purpose. 为此,您可以使用
unique
验证。
public function create(Request $request)
{
$request->validate([
'district_id' => 'reuqired|unique:banks,district_id'
]);
}
Here you are checking if there are any records for this district. 在这里,您正在检查该地区是否有任何记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.