Laravel - Paginate and get()

前端 未结 2 1631
鱼传尺愫
鱼传尺愫 2021-01-04 08:15

With the code below, what I wanted was paginate the query I created. But, when I try to add paginate after get, it throws an error. I wanted to remain get since I want to li

2条回答
  •  春和景丽
    2021-01-04 08:57

    If you look at the method signature you will see that paginate receives a second argument, $columns. So your solution would be to use

    ->paginate($this->limit, $this->fields);
    

    Furthermore, you can clean up your controller by changing things slightly:

    public function index()
    {
    
        $query = Phones::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id');
    
        if ( Request::query('str') ) {
            $query->where('model', 'LIKE', '%'. Request::query('str') . '%')
        }
    
        $phones = $query->paginate($this->limit, $this->fields);
    
        return view('phones.index')->with('phones', $phones);
    }
    

提交回复
热议问题