Call to a member function paginate() on array - php

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);

Related

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

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.

Call to undefined method appends()

I created a filter for db table, but filtration just work for first page
I try this code in controller
public function index(Request $request)
{
$user=User::orderBy('created_at', 'ASC');
if($request->get('search'))
{
if ($request->get('type')) {
$user->where('type', $request->get('type'));
}
if ($request->get('id')) {
$user->where('id', (integer)$request->get('id'));
}
if ($request->get('first_name')) {
$user->where('first_name', $request->get('first_name'));
}
if ($request->get('email')) {
$user->where('email', $request->get('email'));
}
}
$users = $user->paginate(20);
$user->appends(['search' => $request->get('search')]);
return view('admin.user.index', compact('users'));
}
But I get this error
Call to undefined method Illuminate\Database\Query\Builder::appends()
How I can fix this error or how I can make filter works with all pages ?
As I can see you have used following line and paginate the user data in users
$users = $user->paginate(20);
So you need to use users variable instead of user
$users->appends(['search' => $request->get('search')]);

Search all method missing where argument in Laravel 5

I'm trying to write a method in my controller to allow searching the results of my query on my view page. The query selects all results from the "ads" table and this method should allow filtering results by the ad's name inputting keywords in the search bar.
The controller code goes like this:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::paginate(10);
if (!empty($request->input('search_all'))) {
$search_all = urldecode($request->input('search_all'));
$ads->where(function ($query) use ($search_all) {
$query->where('name', 'like', '%'.$search_all.'%')->get();
});
}else {
// Returning to view
return view('admin.ad.list')
->with('ads', $ads)
->with('title', $title);
}
}
However, when I run a search I get the following error: "Missing argument 2 for Illuminate\Support\Collection::where()".
What am I doing wrong?
At the top, when doing $ads = Ad::paginate(10); you have already pulled the results from the database so no more filtering can be done at DB level.
I'd do something like this instead:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::select();
if ($request->input('search_all')) {
$search_all = urldecode($request->input('search_all'));
$ads->where('name', 'like', '%'.$search_all.'%');
}
// Returning to view
return view('admin.ad.list')
->with('ads', $ads->paginate(10))
->with('title', $title);
}

How to count rows of database and show it at laravel 5.4 view?

I'm new at laravel. I'm working on new web application using laravel 5.4. I want to count the number of data in a database and show the result in view. So, I have used this code in controller:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with(['count'=> $count]);
}
and in view:
#if(count($counts)== 0)
no call record to be viewed
#else
<div class="huge">{{$counts}}</div>
#endif
#if(count($count)==0)
no call record to be viewed
#else
<div class="huge">{{$count}}</div>
#endif
but there is an error: undefined variable.
Please resolve this error.
public function bendingImgCount() {
return requestImg::where('is_done', '0')->count();
}
public function uploadedImgCount() {
return = requestImg::where('is_done', '1')->count();
}
public function showDashboard() {
$bendingImgCount = $this->bendingImgCount();
$uploadedImgCount = $this->uploadedImgCount();
return view('/dashboard')->with(compact(['bendingImgCount', 'uploadedImgCount']));
}
Call the showDashboard function and change in the view the counts to bendingImgCount and count to uploadedImgCount.
First you check wherether you get correct data at controller
You just simply
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
dd($counts);
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
dd($count);
return view('/dashboard')->with(['count'=> $count]);
}
And check you get correct answer or not .
I am getting Correct count in browser
1.Controller Page:
2.web.php Page:
3.Web Browser:
If you are even not getting correct value then just show your whole code of page.
Thank you.
TRY THIS:
In Controller:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with('this_is_it', $counts);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with('count_1',$count);
}
In View:
#if(count($this_is_it)== 0)
no call record to be viewed
#else
<div class="huge">{{$this_is_it}}</div>
#endif
#if(count($count_1)==0)
no call record to be viewed
#else
<div class="huge">{{$count_1}}</div>
#endif

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'));
}

Categories