Laravel Multiple Inputs Search 4.2

时间:2016-03-28 14:53:38

标签: laravel search input

我正在进行多输入搜索查询,我需要帮助,因为我不知道如何准确地做到这一点我已阅读所有文档,我不明白它。现在我被困在控制器....

此外,我真的很感激如何在 show.blade 中调用我想要的信息!谢谢你的帮助!

索引刀片

 <div class="panel-body">
        <div class="form-group">
            <div><h4></h4></div>
            <div class="form-group col-md-4">
                {{ Form::open(array('action' => array('UserController@search'), 'class'=>'form width88', 'role'=>'search', 'method' => 'GET')) }}
                <div id="prefetch">
                    {{ Form::text('name', null, array('class' => 'typeahead form-group form-control', 'placeholder' => 'name...')) }}
                    {{ Form::text('lastname', null, array('class' => 'form-group form-control', 'placeholder' => 'lastname...')) }}
                    {{--     {{ Form::text('id', null, array('class' => 'form-group form-control', 'placeholder' => 'id...')) }}
                        {{ Form::text('user-seminars', null, array('class' => 'form-group form-control', 'placeholder' => 'Class tha is enrolled...')) }}
                        {{ Form::text('user-class', null, array('class' => 'form-group form-control', 'placeholder' => 'Class that belongs...')) }}
                        {{ Form::text('user-annex', null, array('class' => 'form-group form-control', 'placeholder' => 'Department that belongs...')) }}
                        {{ Form::text('type', null, array('class' => 'form-group form-control', 'placeholder' => 'User type...')) }}
                        {{ Form::text('date_created', null, array('class' => 'form-group form-control', 'placeholder' => 'date created account...')) }}
                        {{ Form::text('date_enrolled', null, array('class' => 'form-group form-control', 'placeholder' => 'date enrolled in class...')) }}

路由

Route::get('users/index', 'UserController@index');
        Route::get('users/search', 'UserController@search');
        Route::get('users/show', 'UserController@show');

UserContoller

 public function index(){

    return View::make('user.index');
}

public function search(){

    return View::make('user.show');

    }

public function show(){

    return View::make('user.show');

}

USERS TABLE

  

id,firstname,last_name等等

2 个答案:

答案 0 :(得分:1)

public function search(Request $request){   
   $users = App\User::where('firstname',$request->name)
             ->orWhere('lastname',$request->lastname)
             ->orWhere('id',$request->id)
             // more orWhere Clause
             ->get();

    return View::make('user.show',compact('users'));

}

public function show($id){
    $user = App\User::find($id);

    return View::make('user.show',compact('user'));
}

为什么使用返回View :: make('user.show')来渲染两个不同的资源?

答案 1 :(得分:0)

我首先看到它的表单动作

您的代码

 {{ Form::open(array('action' => array('UserController@search'), 'class'=>'form width88', 'role'=>'search', 'method' => 'GET')) }}

 {{ Form::open(array('action' => array('users/search'), 'class'=>'form width88', 'role'=>'search', 'method' => 'GET')) }}

控制器是有趣的部分。如果您正在寻找所需的所有输入,您应该先验证它

//first we set the inputs rultes
$data = Input::all();
$rules = array(
      'name'     => 'required',
      'lastname' => 'required',
      //rest of the inputs  that are required

);
//then we use the validator method
$val = Validator::make($data,$rules);
if($val->fails())
{
      /*redirect to the form again, you can set errors with session 
        or ->withError($validator)*/
      return Redirect::back();
}
then you make your query
$user = User::where('name','=',$data['name'])
        ->where('lastname','=',$data['lastname'])
        //more where('field','=','value')
        ->get();

如果不需要字段,则只能使用

进行查询
$user = User::where('name','=',$data['name'])
        ->orWhere('lastname','=',$data['lastname'])
        //more orWhere('field','=','value')
        ->get();

最后将结果返回到以下视图:

return View::make('user.show')->with('user',$user);