dtby67541 2018-10-25 10:54
浏览 68

我如何从数据库中获取项目并在laravel中的视图中显示

i need the code that will enable me fetch an entire table from the database and load it to my view. and also to the code that will display the item in my views

my controller:

    public function index()
{ 
    if(Auth::user()->usertype=='Admin')
    {
        $categories_count = Categories::count();

        $news_count = News::count();

        $published_news = News::where('status', 1)->count();

        $unpublished_news = News::where('status', 0)->count();

        $slider_news = News::where('slider_news', 'yes')->count();
        $slidsder_news = News::where('slider_news', 'yes')->count();

        $featured_news = News::where('featured_news', 'yes')->count();

        $editor = User::where('usertype', 'Editor')->count();
    }
    else
    {
        $user_id=Auth::user()->id;

        $news_count = News::where(['user_id' => $user_id])->count();

        $published_news = News::where(['user_id' => $user_id, 'status' => '1'])->count();

        $unpublished_news = News::where(['user_id' => $user_id, 'status' => '0'])->count();

    }



    return view('admin.pages.dashboard',compact('categories_count','news_count','published_news','unpublished_news','slider_news','featured_news','editor'));
}
  • 写回答

1条回答 默认 最新

  • dsbezji539113152 2018-10-25 11:01
    关注

    You can simply do

    Model::all();
    

    To get all the data for that model/table. I would recommend paginating this if you have large data sets like so:

    Model::all()->paginate(20);
    

    See pagination here.

    Once you have this assigned to a variable, as you already have done, you can pass it into your compact.

    Within your view, it's always worth checking the collection isn't empty before attempting to loop over:

    @if ($exampleItems->isNotEmpty())
    

    and then you can continue to loop over the collection:

    @foreach ($exampleItems as $exampleItem)
    

    I recommend learning the basics using various tutorials such as Laracasts because if I've understood your request, this is fairly basic.

    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答