0

I am not able to check if there is data in the variable in laravel 6. Here is the function. Here the problem is $tags->count() is not hitting else statement

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

Here is getDataAllWithLimit function

public static function getDataAllWithLimit($table,$order_column,$order_type,$limit){
    $data = DB::table("$table")->orderBy("$order_column", "$order_type")
      ->paginate($limit);
      return $data;
  }
Gamopo
  • 1,600
  • 1
  • 14
  • 22
Upasana Chauhan
  • 948
  • 1
  • 11
  • 32

2 Answers2

0

Try this and hope it helps...

    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);
}
Tohid Dadashnezhad
  • 1,808
  • 1
  • 17
  • 27
0

You can just use PHP's count() to count all the elements.

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);
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95