我正在尝试将搜索表单添加到我的应用程序中。我认为我做的一切都是正确的,但Laravel一直在告诉视图中未定义的$USERS。
下面的表格
<form action="{{URL::to('/students')}}" method="post">
@csrf
<div class="my-3">
<div class="input-group">
<input type="search" name="search" value="{{old('search')}}" class="border border-dark form-control" placeholder="Enter Search Term">
<div class="input-group-text">
<input type="submit" class="btn" value="Search Students">
</div>
</div>
<div class="my-2 text-center">
<span class="text-danger">@error('search'){{$message}}@enderror</span>
</div>
</div>
</form>
用户控制器
public function search(Request $request){
$request->validate([
'search' => 'required',
]);
$query = Students::SELECT('*')
->where('name','LIKE','%'.$request->search.'%')
->orWhere('email','LIKE','%'.$request->search.'%')
->orWhere('department','LIKE','%'.$request->search.'%')
->orWhere('course','LIKE','%'.$request->search.'%')
->get();
return view('students',['users' => $query]);
}
路线
Route::get('/students',function(){
return view('students');
}
Route::post('/students',[UsersController::class,'search']);
内部学生查看视图/Students.blade.php
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Student ID</th>
<th>Stdent Name</th>
<th>Student Email</th>
<th>Student Department</th>
<th>Student Course</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->department}}</td>
<td>{{$user->course}}</td>
<td>
</tr>
</tbody>
</table>
它告诉我$USERS未定义
有人能帮我解决这个问题吗?
2条答案
按热度按时间t3psigkw1#
您现有的GET路由实际上只返回一个视图,没有返回其他任何内容,因此当然没有定义$USERS
daupos2t2#
我就是这么做的。
在路线上
它告诉我$Students是未定义的。如何在路径中传递变量?