需要检查用户在我的网站Laravel 5.2上注册的时间

时间:2016-02-18 22:36:46

标签: php mysql laravel laravel-5.2 php-carbon

我正在我的网站上做奖励系统,我想给已经注册一个月,然后在我的网站上注册多年的用户颁发徽章。我如何在Laravel 5.2中做到这一点?我的用户表上已经有了created_at。

My users table

**********编辑*****************

好的,所以我需要在视图中显示它,类似于:

            @if ($user->difference <= 30)
                <img src="{{ ShowSignedUpFor30days }}" class="ui small circular image" id="Badge">
            @else

            @endif

           @if ($user->difference <= 30)
                <img src="{{ ShowSignedUpFor300days }}" class="ui small circular image" id="Badge">
            @else

            @endif

1 个答案:

答案 0 :(得分:1)

从表中可以看出,您可以使用created_at字段找出用户的创建时间。

您可以在用户模型中创建与此类似的功能以创建徽章。 (未经测试)

public function userSinceInDays(){

  $created = $this->created_at;
  $now = Carbon::now();
  $difference = $created->diff($now)->days;

  return $difference;

}

您可以访问

 @if ($user->userSinceInDays() <= 30)
     <img src="{{ ShowSignedUpFor30days }}" class="ui small circular image" id="Badge">
 @endif

 @if ($user->userSinceInDays() >= 300)
     <img src="{{ ShowSignedUpFor300days }}" class="ui small circular image" id="Badge">
 @endif