mysql Laravel9搜索表单的问题

bzzcjhmw  于 2022-12-10  发布在  Mysql
关注(0)|答案(2)|浏览(124)

我正在尝试将搜索表单添加到我的应用程序中。我认为我做的一切都是正确的,但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未定义
有人能帮我解决这个问题吗?

t3psigkw

t3psigkw1#

Route::get('/students',[UsersController::class,'view']);

您现有的GET路由实际上只返回一个视图,没有返回其他任何内容,因此当然没有定义$USERS

daupos2t

daupos2t2#

我就是这么做的。

public function search(Request $request){
   $query = Students::where('name','LIKE','%'.$request->search.'%')->get();
   return view('index',['students' => $query]);
}

在路线上

use App\Http\Controllers\StudentController;

Route::get('/',function(){
  return view('welcome');
});

Route::get('/'[StudentController::class,'search']);
``
in view at view/welcome.blade.php

它告诉我$Students是未定义的。如何在路径中传递变量?

@foreach($students as $student)
   {{$tudent->id}}
   {{$student->name}}
   {{$student->email}}
@endforeach

相关问题