【问题标题】:Laravel 5.1 - compare Carbon datesLaravel 5.1 - 比较碳日期
【发布时间】:2016-08-09 05:29:26
【问题描述】:

在这里我需要检查我的应用中的文章拍卖是否实时,所以我在文章模型中编写:

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

但现在给我这个错误:

Carbon.php 第 425 行中的 ErrorException:发现意外数据。 发现意外数据。发现意外数据。尾随数据(查看: C:\wamp\www\project\resources\views\articles\index.blade.php)

【问题讨论】:

  • 我也试试: public function scopeCloseauction($query){ $query->where(('to').subDays('auction_end'),'>',Carbon::now()) ; } 但说:调用未定义的函数 App\subDays()

标签: laravel model eloquent blade php-carbon


【解决方案1】:

你可以在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

【讨论】:

    【解决方案2】:

    我认为您的问题在这里:Carbon::parse('to')。你想在这里做什么? Carbon::parse 方法尝试将字符串日期转换为 Carbon 日期。 See the doc。我不认为 Carbon 可以解析字符串'to'。如果你想检查日期之间的差异,you should have a look 到适当的方法,如diffInDays

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多