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

时间:2016-06-23 15:49:21

标签: php sql laravel

首先,我在Laravel上非常新的(noob),而且非常棒。我正在制作一个博客,一切都很棒(以我谦虚和缺乏经验的观点)。

如果有不到24小时前发布的文章,我想在导航栏上显示通知。我已经做到了,但我确定这是一个更好的方法,因为还需要三个SQL查询(每个类别一个)

在我的控制器中

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

然后我将它传递给像这样的视图

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

在视图中我像这样(实际上是部分)

<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>

正如我所说,它运作良好。但我想知道你对绩效的看法以及其他(和更好的)实现这种或良好做法的方法,等等。

结果:

用西班牙语,我翻译了每个变量和路线,让你更容易理解这个想法。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果它对我有用的人有用,我就这样优化:

$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');

结果如下:

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

在视图中

<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>

这样只会使用一个查询,您可以显示新帖子的数量。

感谢willvincent ar Laracasts。