[英]Filter a form within another form for data submission
我在 Laravel 7.4 工作。 我有對應於兩個不同“項目”或“區域”的角色,但它們都在同一個名為“角色”的表中,我們可以用“display_name”字段區分它們,但我希望我的表單創建一個新用戶根據所選的“區域”向我展示角色。
我已經嘗試在我的 controller 和另一個帶有 select 的表單中添加一個條件,但是由於它在提交數據以創建用戶的表單中,這給我帶來了麻煩(問題是數據的發送就像 select 一樣一個提交按鈕,所以它給我一個錯誤,我必須填寫名稱和 email 字段,但我不希望這種情況發生)。 我能做什么?
<form method="POST" action="{{route('User.store')}}">
{{ csrf_field() }}
@include('user.form', ['user'=> new App\User])
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<h3 class="txt-unam text-center text-primary">Roles</h3>
</div>
<div class="my-custom-scrollbar col-md-6">
<table class="table-scroll table table-sm table-striped ">
<thead class="txt-grey text-center">
<tr>
<th class="th-sticky bg-darker text-center">Role</th>
<th class="th-sticky bg-darker text-center">Select</th>
</tr>
</thead>
<tbody>
<form class="row justify-content-center " action="{{url('User')}}">
<select onchange="this.form.submit()" class="col-sm-6 col-12 form-control" name="id" id="selectRole">
<option selected value="" name="servescol">Servescol Roles</option>
<option selected value="" name="indicators">Indicators Roles</option>
</select>
</form>
@foreach($roles as $role)
<tr>
<td class="align-middle small text-center">{{$role->display_name}}</td>
<td class="align-middle text-center"><input type="checkbox" value="{{$role->id}}" id="checkIrRol" name="roles[]" {{$role->pluck('id')->contains($role->id) ? 'checked' : ''}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<br>
<div class="text-center">
<div class="text-center pt-0">
<button class="btn btn-success" type="submit" value="Save">Save</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancel">Cancel</button>
</div>
</div>
</form>
public function create(Request $request)
{
if ($request->get('servescol')) {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
return view('user.create', compact('roles'));
}
if ($request->get('indicators')) {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');
return view('user.create', compact('roles'));
}
$roles = Role::orderby('id')->get();
$departments = Department::all(['id', 'department_name']);
return view('user.create', compact('roles', 'departments'));
}
順便說一句,Departments 與“areas”不同。 條件<= 6
和> 6
根據所屬區域對應id。
使用查詢字符串,你可以做類似的事情
<tbody>
<tr>
<td colspan="2">
<select onchange="location.search = '?type=' + this.value">
<option selected disabled></option>
<option value="servescol">Servescol Roles</option>
<option value="indicators">Indicators Roles</option>
</select>
</td>
</tr>
</tbody>
if ($request->query('type') === 'servescol') {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
return view('user.create', compact('roles'));
}
if ($request->query('type') === 'indicators') {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');
return view('user.create', compact('roles'));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.