Я не могу проверить, есть ли данные в переменной в laravel 6 с коллекцией count () - PullRequest
0 голосов
/ 23 января 2020

Я не могу проверить, есть ли данные в переменной в 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 ]

0 голосов
/ 23 января 2020

Вы можете просто использовать PHP s 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);
}
0 голосов
/ 23 января 2020

Попробуйте и надеюсь, что это поможет ...

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