【问题标题】:Method Illuminate\Database\Eloquent\Collection::links does not exist when searchingMethod Illuminate\\Database\\Eloquent\\Collection::links 搜索时不存在
【发布时间】:2022-11-11 10:36:45
【问题描述】:

搜索某些内容时会出现此问题 -

方法 Illuminate\Database\Eloquent\Collection::links 不存在。

但是分页工作正常并且仅搜索就有问题。

控制器

public function view(Request $request)
    {
        $search = $request['search'] ?? "";
        if($search != ""){
            $customers = Customer::where('name', 'LIKE', "$search%")->orWhere('email', 'LIKE', "%$search%")->get();
        }else{
            $customers = Customer::paginate(10);
        }

        $data = compact('customers','search');
        return view('customer-view')->with($data);
    }

客户view.blade.php

    <form action="" class="col-6">
      <div class="mb-3 d-flex">
        <input type="search" name="search" id="" class="form-control" placeholder="Search by name or email" value="{{ $search }}">
        <button type="submit" class="btn btn-primary ms-1">Search</button>
        <a href="{{ url('/customer/view') }}">
          <button type="button" class="btn btn-primary ms-1">Reset</button></a>
      </div>
    </form>
    <div class="row">
      {{ $customers->links() }}
    </div>

AppServiceProvider.php

class AppServiceProvider extends ServiceProvider{
 public function boot(){
    Paginator::defaultView('vendor.pagination.bootstrap-5');
 }
}

【问题讨论】:

  • 链接不适用于 $customers = Customer::where('name', 'LIKE', "$search%")->orWhere('email', 'LIKE', "%$search%")->get( );这个查询。查询需要对链接进行分页 $customers = Customer::where('name', 'LIKE', "$search%")->orWhere('email', 'LIKE', "%$search%")->分页();

标签: php html laravel


【解决方案1】:

请像下面这样替换您的方法并再次检查。

public function view(Request $request)
    {
        $search = $request['search'] ?? "";
        if($search != ""){
            $customers = Customer::where('name', 'LIKE', "$search%")->orWhere('email', 'LIKE', "%$search%")->paginate(10);
        }else{
            $customers = Customer::paginate(10);
        }

        $data = compact('customers','search');
        return view('customer-view')->with($data);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2020-11-21
    • 2021-09-08
    • 2020-11-13
    • 2019-12-11
    相关资源
    最近更新 更多