我正在尝试为我的Laravel项目创建分页。它将从我的数据库中搜索市场产品并显示数据。 bt我问候调用未定义的方法Illuminate \ Database \ Query \ Builder :: appends()此错误。
这是我的方法
@section('markets-pagination')
@if(isset($searched_market) && $searched_market->count()>0)
{{$searched_market->appends(
[
'category'=>app('request')->input('category'),
'keyword'=>app('request')->input('keyword')
]
)->render()}}
@endif
@stop
这是渲染选项
@section('markets-pagination')
{{$markets->render()}}
@stop
@section('market-name')
@if(isset($market_name))
@foreach($market_name as $item)
<h3>{{$item->market_type}}</h3>
@endforeach
@else
<h3>Showing All Listings</h3>
@endif
@stop
此IS搜索控制器
class SearchController extends Controller
{
private $request;
/**
* @param Request $request
*/
public function search(Request $request)
{
/**
* Some values for the menu etc
*/
$menu_items = MarketType::markets();
//$market_type = MarketType::marketName($id);
$this->request = $request;
/**
* Taking input from the search form fields
*/
$keyword = $this->request['keyword'];
$category = $this->request['category'];
//dd($keyword);
/**
* Retrieving data from the database
*/
//dd($searched_market->items);
if ($keyword != null && $category != null) {
$searched_market = Market::searchBoth($keyword, $category);
} elseif ($keyword != null) {
$searched_market = Market::searchKeyword($keyword);
} elseif ($category != null) {
//dd('both');
$searched_market = Market::searchCategory($category);
} else {
$searched_market = '';
}
//dd($searched_market->toSql());
return view('frontend.search.product')
->with('menu', $menu_items)
->with('searched_market', $searched_market);
}
}
这是类别
public function scopeSearchCategory($query, $category)
{
$this->category = $category;
return $query->join('market_type_id', 'market.id', '=', 'market_type_id.market_id')
->join('market_type', 'market_type.id', '=', 'market_type_id.market_type_id')
->where('market_type.id', $this->category)
->select(['market.*', 'market_type.image'])
->distinct()
->paginate(12);
}
答案 0 :(得分:1)
您不应在scope函数中使用paginate()
或get()
,因为它始终会返回Illuminate\Database\Eloquent\Builder
的实例。
从->paginate(12);
范围中移除scopeSearchCategory
,并在控制器中将其用作:
$searched_market = Market::searchCategory($category)->paginate(12);