我是 Laravel 的初学者,我正在创建一个带有 CRUD 操作的简单 Laravel 项目。现在我有两个数据库表:store 和employee。我已经完成了其他功能,例如创建一个带有姓名、位置等字段的新商店,如员工中那样。
现在,我想在两者之间建立关系,并且我尝试在创建新员工中放置一个下拉列表,其中显示商店数据库表中的商店名称。
有人可以检查我的代码吗?我哪里做错了?
表格
<x-layout>
<x-card class="p-10 mx-auto mt-100">
<div class="bg-gray-50 border border-gray-200 p-10">
<header class="text-center">
<h2 class="text-2xl font-bold uppercase mb-1">
Add an Employee
</h2>
<p class="mb-4">Add a new employee</p>
</header>
<form method="POST" action="/storeEmployeeData">
@csrf
<div class="grid md:grid-cols-2 md:gap-6">
<div class="relative z-0 w-full mb-6 group">
<input type="text" name="name" value="{{old('name')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="name" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Employee Name</label>
@error('name')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror </div>
<div class="relative z-0 w-full mb-6 group">
<input type="text" name="position" value="{{old('position')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="position" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Employee Position</label>
@error('position')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror</div>
</div>
<div class="grid md:grid-cols-3 md:gap-6">
<div class="relative z-0 w-full mb-6 group">
<input type="text" name="phone" value="{{old('phone')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="phone" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Phone Number (09** *** ****)</label>
@error('phone')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror </div>
<div class="relative z-0 w-full mb-6 group">
<input type="text" name="email" value="{{old('email')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="email" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Contact Email</label>
@error('email')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror</div>
<div class="relative z-0 w-full mb-6 group">
<input type="text" name="social" value="{{old('social')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="social" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Social Link</label>
@error('social')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror</div>
</div>
{{-- <div class="relative z-0 w-full mb-6 group">
<input type="text" name="store" value="{{old('store')}}" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " />
<label for="store" class="peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Store</label>
@error('store')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror
</div> --}}
<div class="relative z-0 w-full mb-6 group">
<select name="store_id" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer">
<option value="" disabled selected>Select Store</option>
@foreach($stores as $store)
<option value="{{$store->id}}">{{ $store->name }}</option>
@endforeach
</select>
@error('store_id')
<p class="text-red-500 text-xs mt-1">{{$message}}</p>
@enderror
</div>
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
</div>
</form>
</div>
</x-card>
</x-layout>
员工控制器
class EmployeeController extends Controller
{
public function employeelist()
{
$employees = Employee::latest()->filter(request(['search']))->paginate(5); // Retrieve all items from the database
return view('employees.employeelist', ['employees' => $employees]);
}
public function createEmployee()
{
$stores = Store::all();
return view('employees.createEmployee', compact('stores'));
}
public function storeData(Request $request)
{
$formFields = $request->validate([
'name' => ['required', Rule::unique('employees', 'name')],
'position' => 'required',
'phone' => ['required', Rule::unique('employees', 'phone')],
'email' => ['required', 'email'],
'social' => 'required',
'store_id' => 'required', // Updated to 'store_id' instead of 'store'
]);
Employee::create($formFields);
return redirect('/')->with('message', 'Employee created successfully!');
}
//show edit form
public function editEmployee(Employee $employee)
{
return view('employees.employeeEdit', ['employee' => $employee]);
}
//update store Data
public function update(Request $request, Employee $employee)
{
$formFields = $request->validate([
'name' => ['required'],
'position' => 'required',
'phone' => ['required'],
'email' => ['required', 'email'],
'social' => 'required',
'store_id' => 'required'
]);
$employee->update($formFields);
return back()->with('message', 'Employee details updated successfully!');
}
//delete store data from the database
public function delete(Employee $employee)
{
$employee->delete();
return redirect('/')->with('message', 'Eployee deleted successfully');
}
public function ccreateEmployee()
{
return view('employees.createEmployee');
}
}
路线
Route::get('/createEmployee', [EmployeeController::class, 'createEmployee'])->name('createEmployee');
Route::get('/ccreateEmployee', [EmployeeController::class, 'ccreateEmployee'])->name('ccreateEmployee');
Route::post('/storeEmployeeData', [EmployeeController::class, 'storeData']);
Route::get('/employeelist', [EmployeeController::class, 'employeelist'])->name('employeelist');
Route::get('storeEmployeeData/{employee}/edit', [EmployeeController::class, 'editEmployee']);
Route::put('storeEmployeeData/{employee}', [EmployeeController::class, 'update']);
Route::delete('storeEmployeeData/{employee}', [EmployeeController::class, 'delete']);
不知道为什么在你的控制器中有
createEmployee
和 cccreateEmployee
方法,但看看代码:
public function ccreateEmployee()
{
return view('employees.createEmployee');
}
您没有将
$stores
变量传递给视图