我无法检查 laravel 6 中的变量中是否有数据

I am not able to check if there is data in variable in laravel 6

我无法使用以下函数检查 laravel 6 中的变量中是否有任何数据。

public function view($slug){

    $userdata = Constant_model::getDataOneColumn('users',"username",$slug);

    if (!empty($userdata)) {

        $data = array(
            'title'=>"User Profile - ",
            'description'=>"profile",
            'seo_keywords'=>'',
            'users'=>$userdata
            );

            return view('view_user_profile',$data); 
         }else{

        $data = array(
            'title'=>"Not found ",
            'description'=>"profile",
            'seo_keywords'=>'',
            'users'=>$userdata
            );
            return view('view_user_profile',$data);
    }


}

Constant_model

中的 getDataOneColumn 函数
public static function getDataOneColumn($table, $col1_name, $col1_value){
    $data = DB::table("$table")->where("$col1_name",'=',$col1_value)->get();
      return $data;
  }

如果数据甚至 $userdata 都没有值,则获取。请帮助

使用count:

if ($userdata->count() > 0) {

而不是检查 if (!empty($userdata)) { 您可以检查 laravel 集合,例如 if ($userdata->isNotEmpty()) {

public function view($slug){

    $userdata = Constant_model::getDataOneColumn('users',"username",$slug);

    if ($userdata->isNotEmpty()) { // Change this line

        $data = array(
            'title'=>"User Profile - ",
            'description'=>"profile",
            'seo_keywords'=>'',
            'users'=>$userdata
            );

            return view('view_user_profile',$data); 
         }else{

        $data = array(
            'title'=>"Not found ",
            'description'=>"profile",
            'seo_keywords'=>'',
            'users'=>$userdata
            );
            return view('view_user_profile',$data);
    }


}