2017-06-02 18 views
0

我在我的主页上有一个搜索框,当我点击搜索按钮时,视图不会返回任何结果。laravel 5.3基本网址的发布请求

这是我的形式:

{!! Form::open(array('action' => '[email protected]', 'method' => 'post', 'class' => 'form-inline', 'id' => 'search-bar')) !!} 
{{ csrf_field() }} 
{{Form::select('Location', ['Kasarani','Allsoaps'], null, ['class' => 'form-control', 'placeholder' => 'Location'])}} 

{{ Form::select('Size', ['Bedsitter'], null, ['class' => 'form-control', 'placeholder' => 'Size']) }} 

{{Form::submit('search', array('class' => 'btn btn-success'))}} 
{!! Form::close() !!} 

这是我的路线:

Route::get('/', '[email protected]')->name('home.index'); 
Route::post('/', '[email protected]')->name('home.post.index'); 

这是我的控制器:

public function postIndex(){ 

    $Location = Input::get('Location'); 
    $Size = Input::get('Size'); 

    $validator= Validator::make(
       array(
        'Location'=>$Location, 
        'Size'=>$Size 
        ), 
       array(
        'Location'=>'required', 
        'Size'=>'required' 
       ) 
      ); 
     if ($validator->fails()) { 
      return redirect()->route('home.index')->withErrors($validator); 
      // echo "fail"; 
      }else{ 
      $houses=DB::table('houses')->where('Location', $Location)->where('Size', $Size)->get(); 
      return View('pages.home')->withHouse($houses); 
      } 
} 

有谁知道我缺少的是什么?

+0

如果你'DD($位置);'或'DD($大小); '在postIndex()里面(在重定向之前),你看到什么了吗? –

+0

你可以分享你的视图代码和'房子'表结构吗? – Sandeesh

回答

0

当我DD($位置)我看到 '1'

这是我的观点:

@extends('layouts.app') 

    @section('css') 
     @parent 
     <!-- Custom css --> 
     <link href="custom/css/custom.css" rel="stylesheet"> 
    @stop 

@section('content') 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-12"> 
      <!-- Carousel --> 
      <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
       <!-- Indicators --> 
       <ol class="carousel-indicators"> 
       <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> 
       <li data-target="#carousel-example-generic" data-slide-to="1"></li> 
       <li data-target="#carousel-example-generic" data-slide-to="2"></li> 
       </ol> 

       <!-- Wrapper for slides --> 
       <div class="carousel-inner" role="listbox"> 
       <div class="item active"> 
        <img src="{{asset('images/carousel1.jpg')}}" alt="..."> 
        <div class="carousel-caption"> 
        We are commited 
        </div> 
       </div> 
       <div class="item"> 
        <img src="{{asset('images/carousel2.jpg')}}" alt="..."> 
        <div class="carousel-caption"> 
        To ensuring 
        </div> 
       </div> 

       <div class="item"> 
        <img src="{{asset('images/carousel3.jpg')}}" alt="..."> 
        <div class="carousel-caption"> 
        You get your house of choice 
        </div> 
       </div> 

       </div> 

       <!-- Controls --> 
       <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> 
       <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
       <span class="sr-only">Previous</span> 
       </a> 
       <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> 
       <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
       <span class="sr-only">Next</span> 
       </a> 
      </div> 
      <!-- End of carousel --> 
     </div> 
    </div> 
    <!-- Houses title --> 
    <div class="row"> 
     <div class="col-md-12"> 
      <div class="houses-title-bar"> 
      <h1 class="search-title">Search for your house of choice..</h1> 
      <hr id="hr-position"> 
       <!-- <p> 
        @if (count($errors) > 0) 
         <div class="alert alert-danger"> 
          <ul> 
           @foreach ($errors->all() as $error) 
            <li>{{ $error }}</li> 
           @endforeach 
          </ul> 
         </div> 
        @endif 
       </p> --> 
       <!-- Search bar --> 
       <!-- {!! Form::open(array('url' => ' ', 'method' => 'post', 'class' => 'form-inline', 'id' => 'search-bar')) !!} --> 
       {{Form::open(array('class'=>'form-inline', 'url'=>' ', 'id' => 'search-bar'))}} 
        {{ csrf_field() }} 
        {{Form::select('Location', ['Kasarani','Allsoaps'], null, ['class' => 'form-control', 'placeholder' => 'Location'])}} 

        {{ Form::select('Size', ['Bedsitter'], null, ['class' => 'form-control', 'placeholder' => 'Size']) }} 

        {{Form::submit('search', array('class' => 'btn btn-success'))}} 
       {{ Form::close() }} 
       </form> 
       <!-- End of search bar --> 
      </div> 
     </div> 
    </div> 
    <!-- End of title --> 

    <!-- Houses and details --> 
    <div class="row first-row"> 
     @foreach($house as $house) 
      <div class="col-md-4"> 
       <div> 
        <img src="{{ asset('images/'.$house->Photo) }}" class="img-responsive"> 
       </div> 
       <div class="col-md-12 text-center" id="view-details-button"> 
        <a href="{{ url('all/'.$house->slug) }}" class="btn btn-primary" id="btn-style">View details</a> 
       </div> 
      </div> 
     @endforeach 
    </div> 
    <!-- End of houses and details --> 
</div> 
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="{{URL::asset('bootstrap/js/jquery-1.11.2.js')}}"></script> 
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script src="{{URL::asset('bootstrap/js/bootstrap.min.js')}}"></script> 
@stop 

我的房子表

ID |位置|大小| Created_at | Updated_at

0

我想通了:laravel的奥秘..!问题是在我的形式,最初选择形式返回数组的索引。我做了一个关联数组,现在它返回的实际位置和大小:

这是我的新形式:

{!! Form::open(array(
     'class' => 'form-inline', 
     'url' => ' ', 
     'method' => 'post', 
     'id'  => 'search-bar' 
    )) !!} 
    {{ csrf_field() }} 
    {{ Form::select (
      'Location', 
      [ 
       'Kasarani' => 'Kasarani', 
       'Allsoaps' => 'Allsoaps', 
       'Juja'  => 'Juja' 
      ], 
      null, 
      [ 
       'class'  => 'form-control', 
       'placeholder' => 'Location', 
       'required' => '' 
      ] 
     ) }} 
    {{ Form::select(
      'Size', 
      [ 
       'Bedsitter' => 'Bed sitter', 
       'Onebedroom' => 'One Bedroom', 
       'Twobedroom' => 'Two Bedroom' 
      ], 
      null, 
      [ 
       'class'  => 'form-control', 
       'placeholder' => 'Size', 
       'required' => '' 
      ] 
     ) }} 
    {{ Form::submit('search', array('class' => 'btn btn-success')) }} 
{!! Form::close() !!}