搜索表单/功能没有结果,Laravel

时间:2018-01-12 14:15:43

标签: php laravel

我有这个搜索表单,我的building.blade.php是search.blade.php,问题是它没有给我在搜索栏中搜索到的办公室

当我尝试使用{{dd($ offices)}}时,这就是它的样子;当我点击search.blade.php中的搜索按钮然后我删除它并且它什么都没显示时,我该如何使它工作?

[[1]

search.blade.php

  @extends('layouts.main')
  @section('title', $search)
  @section('content')
 <div class="search">

          {!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
        <div class="input-group col-xs-4 col-md-6" >
          <input type="text" name="search" class="form-control" placeholder="Search...">
         <span class="input-group-btn">
           <button type="submit" class="btn btn-info btn-md">Search
           </button>
          </span>
            </div>
             {!! Form::close()!!}

           </div>
              <hr>
        <table class="table">

          <thead>
              <th>Office Name</th>
              <th>Belongs to</th>
              <th>Office Floor</th>
          </thead>

          <tbody>
            @foreach($offices as $office)
              <tr>
                <td>{{($office)->name}}</td>
                <td>{{$office->building->name}}</td>
                <td>{{$office->floor}}</td>

                <td class="a">

                    @if(!Auth::guest())

                   <a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>
                  <a href="{{route('deleteoffice', $office->id)}}" class="btn btn-danger btn-sm">Delete</a>
                  @endif
                </td>
              </tr>
            @endforeach
          </tbody>
        </table>
      </div>
      </div>
    </div>
        @endsection

Building.blade.php

  @extends('layouts.main')
  @section('title',$building->name)
  @section('css')
  @stop
  @section('content')
 <div class="container">
        <div class="row">
          <div class="col-lg-12">
              <img src="{{URL::to('/assets')}}/{{$building->picture}}" alt="" style="height:300px; width:500px;">
          </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
              {{$building->name}}
            </div>
        </div>
      </div>
    </div>

        <div class="rows">

        <div class="col-md-6 col-md-offset-3">

          <div class="col-xs-4 col-md-6">

  @if(!Auth::guest())
                <a href="{{route('createofficeform', $building->id)}}" class="btn btn-primary btn-md">Create an Office</a>
  @endif
        </div>

          {!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
        <div class="input-group col-xs-4 col-md-6" >
          <input type="text" name="search" class="form-control" placeholder="Search...">
         <span class="input-group-btn">
           <button type="submit" class="btn btn-info btn-md">Search</i>
           </button>
          </span>
            </div>
             {!! Form::close()!!}


        <table class="table">
          <div class="ttitle">
          <thead>
              <th>Office Name</th>
              <th>Office Floor</th>
          </thead>


          <tbody>
            @foreach($offices as $office)
              <tr>

                <td>{{optional($office)->name}}</td>
                <td>{{$office->floor}}</td>
                <td class="a">

                  @if(!Auth::guest())

                  <a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>
                  <a href="{{route('deleteoffice', $office->id)}}" class="btn btn-danger btn-sm">Delete</a>
                  @endif
                </td>
              </tr>
            @endforeach
          </tbody>
        </table>
      </div>
      </div>
        @endsection

OfficeController.php

 public function index()
        {
            $search = \Request::get('search');
    $offices = Office::where('name','like','%'.$search.'%')->get();
return view('search',compact('offices','search'));
      }

Office.php

     namespace App;

use Illuminate\Database\Eloquent\Model;

class Office extends Model
{
    public function building(){
      return $this->belongsTo('App\Building');
    }
}

Building.php

use Illuminate\Database\Eloquent\Model;

class Building extends Model
{
    public $table = 'buildings';

    public function offices(){
      return $this->hasMany('App\Office');
    }
}

路线

Auth::routes();


Route::get('/', 'BuildingController@index')->name('index');

Route::get('building/{id}', 'PageController@show')->name('building');

Route::get('office/{id}', 'OfficeController@show')->name('officeMenu');

Route::get('offices', 'OfficeController@index');


Route::group(['middleware' => ['auth']], function () {

  Route::get('buildings/create', 'BuildingController@create')->name('createbform');

  Route::post('building/create/store', 'BuildingController@saveBuilding')->name('createbuilding');

  Route::get('building/{id}/edit', 'BuildingController@edit');

  Route::post('building/{id}/edit', 'BuildingController@update')->name('editbuilding');

  Route::get('building/{id}/delete', 'BuildingController@destroy');

  Route::get('building/{id}/offices/create', 'OfficeController@create')->name('createofficeform');

  Route::post('building/{id}/offices/create/store', 'OfficeController@store')->name('createoffice');

Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController@edit')->name('editofficeform');

Route::post('building/{id}/offices/{office_id}/edit', 'OfficeController@update')->name('editoffice');


  Route::get('offices/{id}/delete', 'OfficeController@destroy')->name('deleteoffice');
});

2 个答案:

答案 0 :(得分:1)

您应该使用 ILIKE 代替 LIKE ,因为,Office的名称是案例室,您正在寻找办公室名称,其中包含“case”字样。鉴于SQL中的 LIKE 区分大小写,它将无法找到您要查找的内容。

答案 1 :(得分:0)

我没有使用这个<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>,而是将其用作@ oscar.rpr所说的

<a href="{{route('editofficeform', ['id'=>$office->building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>