henryoladj
1 week ago
256
16
Laravel

Problem with getting total number of users and news from database

Posted 1 week ago by henryoladj

I have my database which consists of users tables and news tables.

I would love to get the total number of users and news. and put them on a controller and display it on my view.

NewsController.php

public function index(Request $request, $tag=null)
    {   
        if($tag){
            $tag=Tag::where('name',$tag)->first();
            $news=$tag->news()->latest()->paginate(15);
        }else{
            $news= News::latest()->paginate(15);
        }
        $news = News::withCount('news')->get();
        return view('categories.news',compact(['news','tag']));
    }

PostController.php

public function index(Request $request)
    {
       $news= News::latest()->paginate(15);
       $users = User::latest();
        return view('pages.welcome',compact('news', 'tags', 'users'));
    }

Please sign in or create an account to participate in this conversation.