如何在控制器中使用count()?

发布于 2025-01-23 01:12:42 字数 1124 浏览 2 评论 0原文

如何在这里获得行数?当我使用$ count = $ frignts-> count();我获得此错误计数数组中的所有元素或对象中的所有内容

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 
              

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2025-01-30 01:12:42

使用了分页,因此只需更改

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

There is pagination is used so just change

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

软糖 2025-01-30 01:12:42

尝试使用收集

$data = collect($flights);

,然后您可以使用它来计算

$data->count(); 

try to use collection

$data = collect($flights);

and then you can count it with

$data->count(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文