oliver
oliver

Reputation: 87

how to use count() in controller?

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

Upvotes: 0

Views: 283

Answers (2)

ANAS MK
ANAS MK

Reputation: 131

There is pagination is used so just change

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

Upvotes: 1

Hilmi Hidayat
Hilmi Hidayat

Reputation: 425

try to use collection

$data = collect($flights);

and then you can count it with

$data->count(); 

Upvotes: 0

Related Questions