简体   繁体   English

Laravel - 刀片上出现碳问题

[英]Laravel - carbon problem showing on the blade

I have this car project and I need to show some date of car info at my table.我有这个汽车项目,我需要在我的桌子上显示一些汽车信息的日期。 So this is my code:所以这是我的代码:

<?php $today = Carbon\Carbon::now(); ?>
@if($car->po_datum_isteka == $today)
<td>{{$car->naslov}}</td>
<td class="text-info"><a href="">{{$car->user->name}}</a></td>
<td>{{$car->created_at->format('d-m-Y H:i:s')}}</td>
@endif

So with this code it is not showing anything but with this:因此,使用此代码它没有显示任何内容,但是:

<?php $today = Carbon\Carbon::now(); ?>
@if($car->po_datum_isteka <= $today)
<td>{{$car->naslov}}</td>
<td class="text-info"><a href="">{{$car->user->name}}</a></td>
<td>{{$car->created_at->format('d-m-Y H:i:s')}}</td>
@endif

it shows.表明。 The difference is between == and <= .区别在于==<= Why?为什么? What I'm doing wrong?我做错了什么?

Your problem is that now() returns the current date and time.您的问题是now()返回当前日期和时间。 Which means that you need $car->po_datum_isteka to be the exact same date and time of day.这意味着您需要$car->po_datum_isteka是完全相同的日期时间。

Instead, you can just use the isToday() method.相反,您可以只使用isToday()方法。

@if ($car->po_datum_isteka->isToday())
    <td>{{ $car->naslov }}</td>
    <td class="text-info"><a href="">{{ $car->user->name }}</a></td>
    <td>{{ $car->created_at->format('d-m-Y H:i:s') }}</td>
@endif

This assumes that $car->naslov is an instance of Carbon, which you can ensure by casting it in your model (I'm assuming you have a model called Cars here).这假设$car->naslov是 Carbon 的一个实例,您可以通过将其转换为 model 来确保这一点(我假设您在这里有一个称为 Cars 的 model)。

If $car->po_datum_isteka is not an instance of Carbon, you can always just parse it.如果$car->po_datum_isteka不是 Carbon 的实例,您总是可以只解析它。

@if (\Carbon\Carbon::parse($car->po_datum_isteka)->isToday())
    <td>{{ $car->naslov }}</td>
    <td class="text-info"><a href="">{{ $car->user->name }}</a></td>
    <td>{{ $car->created_at->format('d-m-Y H:i:s') }}</td>
@endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM