0

How do I get the number of rows here? When I use $count = $flights->count(); I get this error Count all elements in an array, or something in an object

public function index(Request $request) { $airline = airline::all(); $search = $request->get('search'); if($search!=""){ $flights = DB::table('flights') ->join('airlines', 'flights.AirlineId', '=', 'airlines.id') ->select('flights.*', 'airlines.name', 'airlines.country','airlines.logo') ->where(DB::raw("CONCAT(flights.id,' ',flights.flightDesignator,' ',airlines.country,' ',airlines.name,' ',flights.departureFrom,' ',flights.arriveTo,' ',flights.departureTime,' ',flights.ArrivalTime)"),'LIKE','%'.$search.'%') ->paginate(4); $flights->appends(['search' => $search]); $count = $flights->count(); if($count == 0) return view('admin.flights')->with(['flights' => $flights,'airline' => $airline, 'NoFound' => 'There is no result 😔']); else return view('admin.flights')->with(['flights' => $flights,'airline' => $airline,'found' => ' records founded']); } } 
3
  • Please improve your code tabbing. Commented Apr 20, 2022 at 5:18
  • 2
    its flights->total() not count(). You are using paginate Commented Apr 20, 2022 at 6:21
  • @ZeroOne yes I just changed it with total() and it's working properly now thanks Commented Apr 20, 2022 at 6:54

2 Answers 2

1

There is pagination is used so just change

$count = $flights->count(); to $count = $flights->total();

Sign up to request clarification or add additional context in comments.

Comments

0

try to use collection

$data = collect($flights); 

and then you can count it with

$data->count(); 

1 Comment

did not work I get this error Object of class Illuminate\Support\Collection could not be converted to int

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.