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

Php 我无法使用count()集合检查laravel 6中的变量中是否有数据,php,laravel,laravel-6,Php,Laravel,Laravel 6,我无法检查laravel 6中的变量中是否有数据。下面是函数。这里的问题是$tags->count()没有命中else语句 public function index(){ $tags = Constant_model::getDataAllWithLimit('tags',"id",'DESC',50); if ($tags->count() >0) { $data = array( 'title'=>'All T

我无法检查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;
  }

试试这个,希望对你有帮助

    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);
}
您可以使用PHP来计算所有元素

公共功能索引()
{
$tags=Constant_model::getDataAllWithLimit('tags','id','DESC',50);
如果(计数($tags)>0){
$data=数组(
'title'=>'All Tags',
'description'=>'All Tags',
“seo_关键字”=>“所有标签”,
'tags'=>$tags
);
返回视图('tags',$data);
}
$data=数组(
'title'=>'未找到页面',
“说明”=>“未找到页面”,
“搜索引擎优化关键词”=>“”,
);
返回视图('404',$data);
}
公共函数getDataAllWithLimit($table、$order\u column、$order\u type、$limit)
{
返回DB::table($table)->orderBy($order\u column,$order\u type)->paginate($limit);
}

plz从变量中删除引号…@TsaiKoga哪一个?
DB::table($table”)
-这仍然有效,PHP在双引号中推断变量,但这不是必需的,
DB::table($table)
更干净。1)$data=DB::table($table)->orderBy($order\u column,“$order\u type”)应该是$data=DB::table($table)->orderBy($order\u column,$order\u type)看起来currentPage是1,perPage是50,所以计数是50,大于0,所以它不会命中else语句。