2016-03-28 88 views
2

我正在做一個多輸入搜索查詢,我需要幫助,因爲我不知道如何完全做到這一點,我已閱讀所有的文件,我不明白it.Now我卡在控制器.....Laravel多輸入搜索4.2

而且我會很感激的如何調用,我想在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('[email protected]'), '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', '[email protected]'); 
     Route::get('users/search', '[email protected]'); 
     Route::get('users/show', '[email protected]'); 

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表

ID,姓名,姓氏,等等等等你爲什麼要使用返回查看

回答

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')); 
} 

::讓( 'user.show')來呈現兩個不同的資源?

+0

我會嘗試這一點,並儘快給您 –

+0

thaxü人是我不需要兩個時間返回視圖::使得它是從實驗^ _ ^,我忘了! –

0

我看到它的形式作用

你的代碼的第一件事

{{ Form::open(array('action' => array('[email protected]'), '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(); 

如果字段的arent要求你只有使查詢與

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

最後你的結果返回給鑑於這樣的:

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

謝謝aswering,但我不認爲我需要做的一切,我從你的答案我需要的信息,我會嘗試它看到的結果! –

+0

thx很多回答@CarlosSalazar –