Home > Backend Development > PHP Tutorial > Laravel显示“New”标签,这里的条件语句怎么写?

Laravel显示“New”标签,这里的条件语句怎么写?

WBOY
Release: 2016-06-06 20:06:18
Original
1015 people have browsed it

我想让更新时间不超过3天的article,旁边显示“New”标签。下面代码中的条件语句怎么写?

<code>    @foreach ($articles as $article)
        <a href="#">{{$article->title}}</a>
        @if($article->updated_at) //这里的条件语句怎么写?
            <span class="label label-danger">New</span>
        @endif
    @endforeach</code>
Copy after login
Copy after login

回复内容:

我想让更新时间不超过3天的article,旁边显示“New”标签。下面代码中的条件语句怎么写?

<code>    @foreach ($articles as $article)
        <a href="#">{{$article->title}}</a>
        @if($article->updated_at) //这里的条件语句怎么写?
            <span class="label label-danger">New</span>
        @endif
    @endforeach</code>
Copy after login
Copy after login

我的一个想法是,在article的model中定义一个方法:

<code>protected $dates = ['published_at'];
public function isNewArticle($time){
  return $time > Carbon::now()->subDay(3) ? true : false;
}</code>
Copy after login

前台判断:

<code>@if($article->isNewArticle($article->updated_at))
...
@endif</code>
Copy after login

代码未经验证,仅仅是思路,可以去尝试一下。或许有其他比较好的方法,共同关注

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template