我是 Laravel 的新手,我有两个表:users、selected_users 都包含 id、name。 我想创建一个由用户填充的下拉列表,当选择用户名时,它将在下拉菜单旁边插入用户名。 当我按下提交时,名称应保存到 selected_user 表中。
请有人帮助编写此代码如何在视图和控制器中编写它。
我不确定你真正想要实现什么,但请尝试下面的代码;
userview.blade.php
<div class="container">
@if(session('success'))
<h1>{{session('success')}}</h1>
@endif
<form method="POST" action="{{route('save.selected-user')}}">
{{ csrf_field() }}
<div class="form-group row">
<div class="col-sm-8">
<select class="form-control" id="selectUser" name="user_selected" required focus>
<option value="" disabled selected>Please select user</option>
@foreach($users as $user)
<option value="{{$user->id}}">{{ $user->name }}</option>
@endforeach
</select>
</div>
<label class="col-sm-4 col-form-label" id="displayUser">Show selected User
here</label>
</div>
<input type="submit" value="Save">
<script type="text/javascript">
var mytextbox = document.getElementById('displayUser');
var mydropdown = document.getElementById('selectUser');
mydropdown.onchange = function(){
mytextbox.value = mytextbox.value + this.value; //to appened
mytextbox.innerHTML = this.value;
}
</script>
TestController.php(确保您有 User 模型和 SelectedUser 模型)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\SelectedUser;
class TestController extends Controller
{
public function populateUsers()
{
$users = User::all();
return view('test.userview', compact('users'));
}
public function saveUser(Request $rq)
{
$selectedUser = new SelectedUser;
$selectedUser->name = $rq->user_selected;
$selectedUser->save();
return redirect()->back()->with('success', 'Selected Username added successfuly');
}
}
WEB.php
Route::get('/selected-user', 'TestController@populateUsers');
Route::POST('/selected-user', 'TestController@saveUser')->name('save.selected-user');
如果有效请告诉我
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
@if (session('status'))
<div class="alert alert-success">{{ session('status') }}</div>
@endif
<div class="card">
<div class="card-header">
<h4>Add Categories
<a href="{{ url('layouts/applayout') }}" class="btn btn-primary float-end">Back</a>
</h4>
</div>
<div class="card-body">
<form action="{{ url('layouts/create') }}" method="POST">
@csrf
<div class="mb-3">
<label>Date</label>
<input type="date" name="Date" value="{{ old('Date') }}" class="form-control" />
@error('Date')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label for="expense_type" class="form-label">Expense Types</label>
<select name="expense_type_id" id="expense_type" class="form-select">
<option value="">Select an Expense Type</option>
@foreach ($expenseTypes as $type)
<option value="{{ $type->id }}"
{{ old('expense_type_id') == $type->id ? 'selected' : '' }}>
{{ $type->name }}</option>
@endforeach
</select>
@error('expense_type_id')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label>Name</label>
<input type="text" name="Name" value="{{ old('Name') }}" class="form-control" />
@error('Name')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label>Purpose</label>
<textarea name="Purpose" class="form-control">{{ old('Purpose') }}</textarea>
@error('Purpose')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label>Amount</label>
<input type="number" name="Amount" value="{{ old('Amount') }}"
class="form-control" />
@error('Amount')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label>Reciept</label>
<input type="text" name="Reciept" value="{{ old('Reciept') }}"
class="form-control" />
@error('Reciept')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
任何人都可以完成这个应该从数据库获取数据的下拉列表吗