Laravel 5 - 仅当 'tip_date' 列等于今天的日期时显示

Laravel 5 - Show only if 'tip_date' column is equal to todays date

我有一个 Laravel 5.1 安装,我有一个包含各种列的提示数据库...一列是提示相关的日期。

因此我只想显示日期等于今天的 table 的行。

我已经设法得到它,所以我可以 运行 一个 foreach 循环遍历每个提示,但需要一个特定的提示来显示与今天日期匹配的提示

任何一个都很棒 :)

这是我的代码


提示控制器

public function index() {
        $tips = Tips::whereDate('tip_date','=', strftime("%y-%m-%d", strtotime(\Carbon\Carbon::now())))->get();
        return view ('tips.index', compact ('tips'));
    }

提示(型号)

protected $fillable = [

        'tip_date',
        'home_team',
        'away_team',
        'league',
        'tip',
        'odds',
        'score',
        'result'

    ];

    protected $dates = ['tip_date'];

Index.Blade.PHP(查看)

<div class="align-center">

                                 <h1><?php echo date("dS F Y");?></h1>


                                 @foreach($tips as $tip)
                                     @if (date("Y-m-d",strtotime($tip['tip_date'])) == date("Y-m-d"))

                                 <h3>{{ $tip->league }}</h3>
                                 <h2>{{ $tip->home_team }} V {{ $tip->away_team }}</h2>
                                 <h3><b>{{ $tip->tip }}</b> @ {{ $tip->odds }}</h3>
                                 @endif
                                 @endforeach


                    </div>
                </div>

                <table class="table table-striped">
                      <thead>
                        <tr>
                          <th>Date</th>
                          <th>Match</th>
                          <th>Tip</th>
                          <th>Odds</th>
                          <th>Score</th>
                          <th>Result</th>
                        </tr>
                      </thead>
                      <tbody>


<?php $tipcount=0; ?>
@foreach($tips as $tip)
    @if (date("Y-m-d",strtotime($tip['tip_date'])) < date("Y-m-d"))
 <tr>
                              <th scope="row">{{ $tip->tip_date }}</th>
                              <td>{{ $tip->home_team }} V {{ $tip->away_team }}</td>
                              <td>{{ $tip->tip }}</td>
                              <td>{{ $tip->odds }}</td>
                              <td>{{ $tip->score }}</td>
                              <td>{{ $tip->result }}</td>
                            </tr>    @endif


    <?php
    $tipcount++;
    if($tipcount===30){break;};
    ?>


@endforeach

您可以获得今天的提示:

$tips = Tip::whereDate('tip_date','=', strftime("%y-%m-%d", strtotime(\Carbon\Carbon::now())))->get();

然后 运行 当前日期可用提示的 foreach 循环。

正如贾马尔所说... 但是在视图/foreach 方面你可以尝试:

@foreach($tips as $tip)
    @if (date("Y-m-d",strtotime($tip['tip_date'])) == date("Y-m-d"))
        {{$tip->name}}
    @endif
@endforeach

<?php $tipcount=0; ?>
@foreach($tips as $tip)
    @if (date("Y-m-d",strtotime($tip['tip_date'])) < date("Y-m-d"))
        {{$tip->name}}
    @endif


    <?php 
    $tipcount++; 
    if($tipcount===30){break;}; 
    ?>  
@endforeach

如果您需要在代码中的多个视图中执行此检查,您可以在 Tip 模型中实施 isToday() 方法。

在你的模型中

public function isToday()
{
    return $this->date == date('Y-m-d');
}

假设 $tips 已经是从任何地方正确检索到的 collection。

在您的 blade 视图中

@foreach($tips as $tip)
    @if($tip->isToday())
       /* whatever you want to display */
    @endif
@endforeach

当然这是一个示例,您可能希望根据您的命名约定和字段格式调整日期检查,例如使用 Carbon。

Advice if your tips collection is actually a relationship of another model then your for might change a bit, depending on how you called the relationship and how are you retrieving them.