Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links() - php

I am using Laravel Framework 8.62.0 and PHP 7.4.20.
I get the following error:
Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: /home//Code/test_project/resources/views/index.blade.php)
I have a view that has uses 3 simple filters. To display the view via get I use the following:
public function getSearchView()
{
try {
$con = 'mysql_prod';
// search results
$items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
->leftJoin('images', 'images.items_id', '=', 'items.id')
->limit(500)
->paginate(10);
// filter field
$condition = Condition::on($con)->select(['id', 'name AS condition_name'])
->distinct()
->get();
$collection = Collection::on($con)->select(['id', 'name AS collection_name'])
->distinct()
->orderBy('collection_name', 'ASC')
->get();
return view('index', compact('items'));
} catch (\Exception $e) {
Log::error($e);
report($e);
}
}
To filter the view I use:
public function postFilter(Request $request)
{
try {
$con = 'mysql_prod';
//##################################
// QUERY - SEARCH RESULTS
//##################################
$items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
->leftJoin('images', 'images.items_id', '=', 'items.id');
// collection
if(!is_null($request->select_collection_field)) $items->where('collections.id', '=', intval($request->select_collection_field));
// FILTER field
if(!is_null($request->select_filter_field)) {
if($request->select_filter_field === "select_all") $items->orderBy('item_name', 'desc');
if($request->select_filter_field === "publishing_year") $items->orderBy('collections_year', 'desc');
}
// query database
$items->limit(500)->paginate(10);
//##################################
// FILTERS
//##################################
$condition = Condition::on($con)->select(['id', 'name AS condition_name'])
->distinct()
->get();
$collection = Collection::on($con)->select(['id', 'name AS collection_name'])
->distinct()
->orderBy('collection_name', 'ASC')
->get();
return view('index', compact('items', 'condition', 'collection'));
} catch (\Exception $e) {
Log::error($e);
report($e);
}
}
In my web.php I have the two endpoints:
Route::get('/', [SearchController::class, 'getSearchView'])->name('/');
Route::post('postFilter', [SearchController::class, 'postFilter']);
In my view I use the pagination of laravel:
{!! $items->links('vendor.pagination.default') !!}
Any suggestions why I get the above error and how to fix it?
I appreciate your replies!

$items is currently a Query Builder instance. This object wont change, it will continue to be a Query Builder instance. When you execute a query from a Query Builder you get a returned result, and that is what you need to be passing to your view. You could reassign $items to this result easily:
$items = $items->limit(500)->paginate(10);
Now $items is the Paginator instance because you reassigned that variable to the result of the paginate call.

public function boot()
{
Paginator::defaultView('view-name');
Paginator::defaultSimpleView('view-name');
}
add this code to AppServiceProvider. I hope it will work.

Related

Laravel function query() "pagination does not exist"

Just wondering where to position the Laravel's pagination() here in my query() in order for it to work. I tried adding it at the end of $all_procedures:
public function fetchprocedure(Request $search_procedure) {
$filter_procedure = $search_procedure->input('search_procedure');
$all_procedures = HmsBbrKnowledgebaseProcedure::query()
->when(is_null($filter_procedure), function ($query) {
$query->where('bbr_procedure_id', '>=', 1);
})
//iLike is case insensitive
->when(!empty($filter_procedure), function ($query) use ($filter_procedure) {
$query->where('procedure_name', 'iLIKE', "%$filter_procedure%");
})
->orderBy('bbr_procedure_id', 'ASC')
->get()
->paginate(10);
return response()->json([
'all_procedures' => $all_procedures,
]);
}
UPDATE:
tried a solution given by an answer. Got rid of get(); but my error this time is:
Just instead of having:
->get()
->paginate(10);
Do
->paginate(10);
That will automatically get 10 results and paginate it taking into account the current page.

Filtering in Laravel using regex

I'm trying to filter products based on query string. My goal is to get products from a collection if it's given, otherwise get every product. Could someone help me what's wrong with the following code?
$products = \App\Product::where([
'collection' => (request()->has('collection')) ? request('collection') : '[a-z]+',
'type' => (request()->has('type')) ? request('type') : '[a-z]+'
])->get();
PS.: I've also tried with 'regex:/[a-z]+', it's not working...
$products = \App\Product::where(['collection' => (request()->has('collection')) ? request('collection') : 'regex:/[a-z]+'])->get();
What you can do is use when eloquent clause, so your where clause for collections will be triggered only when the request('collection') exists, same logis applie to type as well.
$products = \App\Product::
when(request()->has('collection'), function ($q) {
return $q->where('collection', request('collection'));
});
->when(request()->has('type'), function ($q) {
return $q->where('type', request('type'));
})
->get();
Or another way if you have your request values assigned to a variable something like:
$collection = request('collection');
$type= request('type');
$products = \App\Product::
when(!empty($collection), function ($q) use ($collection) {
return $q->where('collection', $collection);
});
->when(!empty($type), function ($q) use ($type) {
return $q->where('type', $type);
})
->get();

Call to a member function paginate() on array

I am working with Laravel5.0 . I want to add paginate() to the following function of my controller part.
public function index()
{
try{
$val=DB::connection()->getDatabaseName();
if(DB::connection()->getDatabaseName()) {
//$bRecord = DB::table('bills')->orderBy('Date', 'desc')->paginate(4);
$bRecord = DB::table('bills')
->join('clients', 'bills.ClientID', '=', 'clients.ClientID')
->select('bills.ReceiptID', 'bills.ClientID', 'bills.Paid', 'bills.Date','bills.ReceivedBy',
'clients.ClientName')
->get();
return view('bills.billRecord')->with('bRecord', $bRecord);
}else{
$er="/connection status: database error";
return view('home')->with('error',$er); //'error' is passed to home
}
}catch (\Exception $e){
$er="/connection status: database error";
return view('home')->with('error',$er);
}
}
if I add paginate here, it shows error. "Call to a member function paginate() on array"
$bRecord = DB::table('bills')
->join('clients', 'bills.ClientID', '=', 'clients.ClientID')
->select('bills.ReceiptID', 'bills.ClientID', 'bills.Paid', 'bills.Date','bills.ReceivedBy',
'clients.ClientName')
->get()->paginate(4);
How can I use paginate() here?
Example from Laravel 5.0 Pagination documentation:
$users = DB::table('users')->paginate(15);
So try to change last line of your example from
->get()->paginate(4);
to
->paginate(4);

Uses Tabs in Blade with Same Route, Different Methods in Laravel

I'm using tabs but same controller, different methods. How to return values to different views with same route?
In /users, get value from db via BuyerSellerController#buyers method for buyer.
Route::get('users','BuyerSellerController#buyers');
In /users as well, get value from db via BuyerSellerController#sellers method for seller.
Route::get('users','BuyerSellerController#sellers');
//BuyerSellerController
public function buyers()
{
$buyerSeller = DB::table('buyerseller')
->where('buyerseller','=','Buyer')
->pluck('id');
$buyers = DB::table('users')
->where('buyerseller','=',$buyerSeller)
->get();
return View::make('pages.users')->with('buyers', $buyers);
}
public function sellers()
{
$buyerSeller = DB::table('buyerseller')
->where('buyerseller','=','Seller')
->pluck('id');
$sellers = DB::table('users')
->where('buyerseller','=',$buyerSeller)
->get();
return View::make('pages.users')->with('sellers', $sellers);
}
//users.blade.php
Then I got this error:
Undefined variable: sellers (View: ...)
compact saved my life! :D
public function index()
{
/* buyers */
$buyerSeller = DB::table('buyerseller')
->where('buyerseller','=','Buyer')
->pluck('id');
$buyers = DB::table('users')
->where('buyerseller','=',$buyerSeller)
->get();
/* sellers */
$buyerSeller = DB::table('buyerseller')
->where('buyerseller','=','Seller')
->pluck('id');
$sellers = DB::table('users')
->where('buyerseller','=',$buyerSeller)
->get();
return View::make('pages.users', compact('buyers', 'sellers'));
}

Paginate an Eloquent query with join, where and orderBy-laravel 4

I have following ORM
$trucksobj = DB::Table('trucks')
->orderBy('trucks.id','desc')
->join('trucktypes', 'trucks.trucktype_id', '=', 'trucktypes.id');
if($overweight=="on")
$trucksobj->where('trucktypes.max_weight', '>', 'trucks.weight');
$trucks=$trucksobj->get();
It work like a charm, but when i replace get() with paginate() it is not responding i.e
$trucks=$trucksobj->paginate(5);
is not working?
any thing i have missed?
Please follow this :http://culttt.com/2014/02/24/working-pagination-laravel-4/
Please us paginator function when you use manual query:
$trucksobj = DB::Table('trucks')
->orderBy('trucks.id','desc')
->join('trucktypes', 'trucks.trucktype_id', '=', 'trucktypes.id');
if($overweight=="on")
$trucksobj->where('trucktypes.max_weight', '>', 'trucks.weight');
$trucks=$trucksobj->get();
//$paginator = Paginator::make($items, $totalItems, $perPage);
$paginator = Paginator::make($trucks, count($trucks), 5);
public function pagination()
{
$users = DB::table('img')
->join('category', 'category.id', '=', 'img.category_id')
->select('img.category_id', 'category.name', 'img.img_url')
->paginate(2);
return view('page', compact('users'));
}

Categories