Reputation: 87
I am trying to create a pagination for my Laravel project which will search market products from my database and show data. But I am getting the below error:
Call to undefined method Illuminate\Database\Query\Builder::appends()
Here is my Method:
@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
Here is Rendering Option:
@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
This is Search Controller:
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);
} else if ($keyword != null) {
$searched_market = Market::searchKeyword($keyword);
} else if ($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);
}
}
This is the category:
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);
}
Upvotes: 2
Views: 6019
Reputation: 17668
You should not use paginate()
or get()
in your scope functions as it will always returns an instance of Illuminate\Database\Eloquent\Builder
.
Remove ->paginate(12);
from your scopeSearchCategory
scope and use it in your Controller as:
$searched_market = Market::searchCategory($category)->paginate(12);
Upvotes: 2