为了账号安全,请及时绑定邮箱和手机立即绑定

我无法使用 count() 集合检查 laravel 6 中的变量中是否有数据

我无法使用 count() 集合检查 laravel 6 中的变量中是否有数据

PHP
白猪掌柜的 2022-07-29 16:10:25
我无法检查 laravel 6 中的变量中是否有数据。这是函数。这里的问题是$tags->count()没有打 else 语句public function index(){    $tags = Constant_model::getDataAllWithLimit('tags',"id",'DESC',50);    if ($tags->count() >0) {        $data = array(            'title'=>'All Tags',            'description'=>'All Tags',            'seo_keywords'=>'All Tags',            'tags'=>$tags        );        return view('tags',$data);     }else{        $data = array(            'title'=>"Page Not found",            'description'=>"Page not found",            'seo_keywords'=>'',            );            return view('404',$data);    }}这是 getDataAllWithLimit 函数public static function getDataAllWithLimit($table,$order_column,$order_type,$limit){    $data = DB::table("$table")->orderBy("$order_column", "$order_type")      ->paginate($limit);      return $data;  }
查看完整描述

2 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

试试这个,希望它有帮助......


    if (count($tags->toArray()['data']) > 0) {


    $data = array(

        'title'=>'All Tags',

        'description'=>'All Tags',

        'seo_keywords'=>'All Tags',

        'tags'=>$tags

    );


    return view('tags',$data); 


}else{

    $data = array(

        'title'=>"Page Not found",

        'description'=>"Page not found",

        'seo_keywords'=>'',

        );


        return view('404',$data);

}


查看完整回答
反对 回复 2022-07-29
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

您可以只使用 PHP 的count()来计算所有元素。


public function index()

{

    $tags = Constant_model::getDataAllWithLimit('tags', 'id', 'DESC', 50);


    if (count($tags) > 0) {


        $data = array(

            'title' => 'All Tags',

            'description' => 'All Tags',

            'seo_keywords' => 'All Tags',

            'tags' => $tags

        );


        return view('tags', $data);

    }


    $data = array(

        'title' => 'Page Not found',

        'description' => 'Page not found',

        'seo_keywords' => '',

    );


    return view('404', $data);

}

public function getDataAllWithLimit($table, $order_column, $order_type, $limit)

{

    return DB::table($table)->orderBy($order_column, $order_type)->paginate($limit);

}


查看完整回答
反对 回复 2022-07-29
  • 2 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信