【问题标题】:Laravel 5 - Show only if 'tip_date' column is equal to todays dateLaravel 5 - 仅在“tip_date”列等于今天日期时显示
【发布时间】:2015-07-22 19:17:41
【问题描述】:

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

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

我已经设法得到它,所以我可以运行一个 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

【问题讨论】:

  • 是否可以选择使用数据库仅返回今天的行?还是您需要返回所有记录然后解析它们以获取今天的提示?
  • 如果你得到今天的所有提示而不是得到所有提示然后通过循环过滤会更好。
  • 我需要让今天的提示显示在页面顶部,然后在表格中显示最后 30 个提示,不包括下面的今天,都在同一页面/视图上

标签: model-view-controller controller laravel-5 laravel-5.1


【解决方案1】:

正如贾马尔所说... 但在视图/ 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

【讨论】:

  • 这已经很完美了,现在我需要在下面的同一视图中创建一个表格,显示除今天日期之外的 30 个之前的提示...有什么想法吗?
  • 查看我编辑的答案以包含额外表格的选项。这有点小技巧,但应该可以完成这项工作!
  • 无法让第二部分工作它不会在表格中显示结果:(
  • 编辑您的问题以发布相关代码,我们可以看看。
  • 代码看起来很好——我已经在我这边测试过了,一切都很好,所以它可能与数据相关。在某处显示记录集计数,以便您可以确定控制器传递了实际记录。更改第二个 foreach 的参数(如 if 1==1)以检查代码是否实际进入该循环。
【解决方案2】:

您可以获得今天的提示:

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

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

【讨论】:

    【解决方案3】:

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

    在你的模型中

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

    假设$tips 已经是从您拥有它的任何地方正确检索到的集合。

    在您的刀片视图中

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

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

    建议如果您的提示集合实际上是另一个模型的关系,那么您的 for 可能会有所改变,具体取决于您如何调用该关系以及如何检索它们。

    【讨论】:

    • 在尝试这个之后,我得到以下错误....'Call to undefined method Illuminate\Database\Eloquent\Collection::isToday()'
    • 你在哪里使用它?你称它为静态的,那不是这样的。我将添加更多代码。
    • 你的小费是另一个模型的一部分(关系)吗?
    猜你喜欢
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多