为什么Laravel 5.6应用程序中的最新方法不起作用?

时间:2018-11-04 05:37:33

标签: php laravel search pagination

使用laravel 5.6,在我的应用中,我使用algolia在索引刀片中搜索输入。 index.blade.php

<form method="GET" action="{{ url('search') }}">
      {{csrf_field()}}
            <div class="row">
                <div class="col-md-6">
                    <input type="text" name="search" class="form-control" placeholder="Search">
                </div>
                <div class="col-md-6">
                    <button class="btn btn-info">Search</button>
                </div>
            </div>
        </form>

        <br> 

@forelse( $vehicles as $vehicule )


                    @if( $vehicule->uploads->count() > 0 )
                        <a href="{{ route('vehicles.show', $vehicule->id) }}">
                            @php
                                $upload = $vehicule->uploads->sortByDesc('id')->first();
                            @endphp

                            <div style="border-style: solid; background-color: {{ $vehicule->adtype === 1 ? '#FFEFD5' : '#FFFFFF' }} ">
                            <img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>


                        {{ Carbon\Carbon::parse($vehicule->created_at)->diffForHumans()}} 
                            {{$vehicule->provincename}}
                          {{$vehicule->milage}}

                          </div>
<br>
                           <hr>

                    @endif

            @empty
                <td>No Advertisment to display.</td>
            @endforelse

</div>

</div>
</div>
{{ $vehicles->links() }}
</div>
@endsection

我已经设置了索引刀片文件的分页。

这是SearchController @ search函数。

public function search(Request $request)
    {
$vehicles =  Vehicle::search($request->get('search'))->orderBy('adtype','DESC')

            ->latest('updated_at')
            ->paginate(10);
        return view('vehicles.index', compact('vehicles'));
    } 

但是当我在搜索输入中输入一些值并输入搜索按钮

following error is occurred.
1/1) BadMethodCallException

Method latest does not exist.

如何解决此问题?

修改dd结果

Builder {#375 ▼
  +model: Vehicle {#372 ▶}
  +query: "gampaha"
  +callback: null
  +index: null
  +wheres: []
  +limit: null
  +orders: array:1 [▶]
}

更新的车辆型号

use Searchable;
     protected $guarded = [];

    public function searchableAs()
    {
        return 'categoryname';
    }

1 个答案:

答案 0 :(得分:0)

尝试一下并告诉您将会得到什么

Vehicle::where('column1', 'like', '%' . $request->get('search') . '%')->orWhere('column2', 'like', '%' . $request->get('search') . '%')->orderBy('adtype', 'DESC')   ->latest('updated_at')   ->paginate(10);

并在“车辆”模型中共享您的搜索方法。