doujiao4710 2016-06-23 15:49
浏览 22
已采纳

如果有新帖子,请在菜单上标记

First of all, I'm pretty new (noob) on Laravel, and it's awesome. I'm making a blog, and everything is great (In my humble and inexperienced opinion).

I want to display a notification on the navbar if there are articles posted less than 24 hours ago. I've made it, but I'm sure there's is a much better way, because three more SQL queries are needed (One for each category)

In my controller

$newPostEntrevistas = FALSE;
$newPostAcercaDeMi = FALSE;
$newPostEstiloDeVida = FALSE;

$lastPostInterviews = Post::where('category', 'Interviews')->orderBy('created_at', 'desc')->first(['created_at']);
$lastPostAboutMe = Post::where('category', 'About me')->orderBy('created_at', 'desc')->first(['created_at']);
$lastPostLifestyle = Post::where('category', 'Lifestyle')->orderBy('created_at', 'desc')->first(['created_at']);

    if ($lastPostInterviews->created_at->diffInHours() < 24) {
        $newPostInterviews = TRUE;
    }

    if ($lastPostAboutMe->created_at->diffInHours() < 24) {
        $newPostAboutMe = TRUE;
    }

    if ($lastPostLifestyle->created_at->diffInHours() < 24) {
        $newPostLifestyle = TRUE;
    }

Then I pass it to a view like this

$posts = Post::orderBy('created_at', 'desc')->paginate($this->paginationNumber());
        return view('pages.index', [
            'posts' => $posts,
            'newPostInterviews' => $newPostinterviews,
            'newPostboutMe' => $newPostAboutMe,
            'newPostLifestyle' => $newPostLifestyle,
        ]);

And in the view I catch it like this (In a partial, actually)

<nav>
    <div class="nav-wrapper">
        <ul>
            <li><a href="/"><i class="fa fa-home"></i> Home</a></li>
            <li>
                <a href="/interviews">
                    Interviews
                    @if($newPostInterviews)
                        <i class="fa fa-exclamation-circle"></i>
                    @endif
                </a>
            </li>
            <li>
                <a href="/lifestyle">
                    Lifestyle
                    @if($newPostLifestyle)
                        <i class="fa fa-exclamation-circle"></i>
                    @endif
                </a>
            </li>
            <li>
                <a href="/about-me">
                    About me
                    @if($newPostAboutMe)
                        <i class="fa fa-exclamation-circle"></i>
                    @endif
                </a>
            </li>
        </ul>
    </div>
</nav>

As I said, it works fine. But I want to know your opinions on performance and other (and better) ways to achieve that or good practices, etcetera.

Result:

It's in spanish, I translated every variable and route to make it easier for you to understand the idea.

enter image description here

  • 写回答

1条回答 默认 最新

  • drhgzx4727 2016-06-23 22:53
    关注

    If it's useful for someone I optimized it like this:

    $newPosts = DB::table('posts')
                  ->where('created_at', '>=', Carbon::now()->subHours(24)->toDateTimeString())
                  ->groupBy('category')
                  ->select('category', DB::raw('COUNT(category) as number'))
                  ->pluck('number', 'category');
    

    The result is something like:

    [
      'Interviews' => 5,
      'Lifestyle' => 7,
      'About me' => 1,
    ]
    

    And in the view

    <nav>
        <div class="nav-wrapper">
            <ul>
                <li><a href="/"><i class="fa fa-home"></i> Home</a></li>
                <li>
                    <a href="/interviews">
                        Interviews
                        @if(isset($newPosts['Interviews']))
                            <span class="badge">{{ $newPosts['Interviews'] }}</span>
                        @endif
                    </a>
                </li>
                <li>
                    <a href="/lifestyle">
                        Lifestyle
                        @if(isset($newPosts['Lifestyle']))
                            <span class="badge">{{ $newPosts['Lifestyle'] }}</span>
                        @endif
                    </a>
                </li>
                <li>
                    <a href="/about-me">
                        About me
                        @if(isset($newPosts['About me']))
                            <span class="badge">{{ $newPosts['About me'] }}</span>
                        @endif
                    </a>
                </li>
            </ul>
        </div>
    </nav>
    

    That way only one query is used and you can show the number of the new posts.

    Thanks to willvincent ar Laracasts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大