Laravel 5.1 - 比较碳日期

Laravel 5.1 - compare Carbon dates

这里我需要检查我的应用程序中文章的拍卖是否在进行中,所以我在文章模型中写道:

public function scopeCloseauction($query){
        $query->where(Carbon::parse('to')->subDays('auction_end'),'>',Carbon::now());
    }

我的观点是:

@if ($article->Closeauction())
                            <td>
                           auction is live
                        </td>
                            @else
                            <td>
                             auction is closed
                            </td>
                        @endif

但是我有一个问题,因为我得到了错误:

更新: 我也尝试: 在模型中添加功能:

public function isLive($to,$auction_end) {
    $first = Carbon::create($to).subDays($auction_end);
    $second = Carbon::now();
        return ($first->lte($second));
    }

并且在视图中:

@if ($article->isLive($article->to,$article->auction_end))
                            <td>
                            live
                        </td>
                            @else
                            <td>
                                closed
                            </td>
                        @endif

但现在给我这个错误:

ErrorException in Carbon.php line 425: Unexpected data found. Unexpected data found. Unexpected data found. Trailing data (View: C:\wamp\www\project\resources\views\articles\index.blade.php)

我认为您的问题出在这里:Carbon::parse('to')。你想在这里做什么? Carbon::parse 方法尝试将字符串日期转换为 Carbon 日期。 See the doc. I dont think that Carbon can parse the string 'to'. If you want to check the diff between dates, you should have a look 到适当的方法,如 diffInDays

您可以做一些事情,将这样的功能添加到您的 Article 模型中:

public function isLive() 
{
    $first = Carbon::parse($this->to)->subDays($this->auction_end);
    $second = Carbon::now();
    return $first->lte($second);
}

现在在您看来您可以使用:

@if ($article->isLive())
    <td>
      live
    </td>
    @else
    <td>
        closed
    </td>
@endif