【问题标题】:Search data from database in textbox to display - Laravel在文本框中从数据库中搜索数据以显示 - Laravel
【发布时间】:2018-12-06 04:03:05
【问题描述】:

我的问题是我无法搜索,也无法显示我的 texboxes 中的值。

我想要的是搜索每个用户的 id 并将其数据显示到我的文本框

如何在 Laravel 的视频 This Video 上做到这一点?

到目前为止,我有 这个

这是我的页面

查看

 {!! Form::open(['action' => 'Admin\EmployeeFilemController@search', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}

                    <input type="text" name="id" class="form-control" placeholder="Enter ID to Search"><br>
                    <input type="submit" class="btn btn-primary btn-md" name="search" value="Search Data">
      {!! Form::close() !!}

控制器

   public function search(Request $request){
    $output = "";
    $employees = DB::table('employeefms')->where('id')->get();
    return redirect('/admin/employeemaintenance');
}

我的查看输入

 <div class="form-group col-md-2">
                {{Form::label('employee_no', 'Employee No.')}}
                {{Form::text('employee_no', '',['class' => 'form-control', 'placeholder' => 'Employee No.'])}}
        </div>

    <div class="row">
        <div class="form-group  col-md-4">
                {{Form::label('last_name', 'Last Name')}}
                {{Form::text('last_name', '',['class' => 'form-control', 'placeholder' => 'Last Name'])}}
        </div>

        <div class="form-group  col-md-4">
                    {{Form::label('first_name', 'First Name')}}
                    {{Form::text('first_name', '',['class' => 'form-control', 'placeholder' => 'First Name'])}}
        </div>
    </div>

    <div class="row">
        <div class="form-group  col-md-4">
                    {{Form::label('middle_name', 'Middle Name')}}
                    {{Form::text('middle_name', '',['class' => 'form-control', 'placeholder' => 'Middle Name'])}}
        </div>

        <div class="form-group  col-md-4">
                    {{Form::label('nick_name', 'Nick Name')}}
                    {{Form::text('nick_name', '',['class' => 'form-control', 'placeholder' => 'Nick Name'])}}
        </div>
    </div>

【问题讨论】:

    标签: php mysql laravel laravel-5 eloquent


    【解决方案1】:

    您似乎没有传递用户在控制器函数中输入的id

    $employees = DB::table('employeefms')->where('id')->get();
    

    您可能需要进行以下更改

    $input = $request->all();
    $id = $input['id']
    // $employees = DB::table('employeefms')->where('id', $id)->get();
    
    // actually, if 'id' is the primary key, you should be doing
    $employee = DB::table('employeefms')->find($id);
    
    // now pass the data to the view where you want to display the record
    // like so
    return view('name_of_view', compact('employee'));
    

    然后,使用 Laravel 的 Form-Model 绑定

    {!! Form::model($employee,
                   ['action' => ['Admin\EmployeeFilemController@update', $employee->id],
                   'method' => 'patch' // or whatever method you have defined
                   ]) !!}
    
               // your form fields specified above will go here
    {!! Form::close() !!}
    

    【讨论】:

    • 我应该改变视图值 {{form::text...}} 还是不?
    • 你可以使用 Laravel 的 'Form-Model binding'。我会更新我的答案。
    • 好吧,等一下,这就是我所缺少的,我不知道如何在控制器上绑定它的语法,谢谢
    • 嘿,先生,我遇到了一个错误Undefined variable: employee
    • 然后改成$employees。看看你在声明$employees = DB::table('employeefms')-&gt;where('id', $id)-&gt;get(); 中给出了什么名字
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多