繁体   English   中英

如何根据 Laravel 中的日期获得不同的视图

[英]How to have different view based on date in Laravel

通过比较今天的日期和过期日期,我在获得不同的观点时遇到了问题。 在我更深入地讲述我的问题之前,让我向您展示代码:

买家表

id
name
address
phone_no
email
expired_at (date)
created_at

买家控制器.php

public function create(Request $request)
{
   $buyers = Buyer::create([
       'name' => $request->name,
       'address' => $request->address,
       'phone_no' => $request->phone_no,
       'email' => $request->email,
       'expired_at' => Carbon::today()->addDays(730),
   ]);

   return response()->json($buyers);
}

买家\\index.blade.php

<div class="panel-body">
   <table class="table table-hover" id="buyer-table">
       <thead>
           <tr>
               <th>ID</th>
               <th>Name</th>
               <th>Address</th>
               <th>Phone No</th>
               <th>Email</th>
               <th>Registered Date</th>
               <th>Liability Period</th>
           </tr>
       </thead>
       @foreach($buyers as $buyer)
          <tr class="buyer{{$buyer->id}}">
              <td>{{$buyer->buyer_id}}</td>
              <td>{{$buyer->name}}</td>
              <td>{{$buyer->address}}</td>
              <td>{{$buyer->phone_no}}</td>
              <td>{{$buyer->email}}</td>
              <td>{{date('d/m/Y', strtotime($buyer->created_at))}}</td>
              @if(now() < $buyer->expired_at)
                 <td class="text-success">Valid</td>
              @else
                 <td class="text-danger">Expired</td>
              @endif
          </tr>
        @endforeach
   </table>
</div>

到这里为止,我设法完成了我的代码,我成功地在表视图中比较了过期日期和今天的日期。

网页.php

 Route::get('/report-form','ComplaintController@create');

投诉控制器.php

public function create()
{
   return view('buyers.complaints.create');
}   

但是,在决定开发这个新想法之前,买家可以查看create.blade.php 以进行新的投诉。 但是现在,如果expired_at已经过了今天的日期,买家不能提出任何投诉,将返回查看expired.blade.php。

买家\\投诉\\create.blade.php

<div class="panel-heading">
    <h3 class="panel-title"><strong>Make New Report</strong></h3>
</div>

//There is a form for the buyers to fill in the complaint

买家\\投诉\\expired.blade.php

<div class="panel-heading">
    <h3 class="panel-title"><strong>Sorry you can't make any report</strong></h3>
</div>

我只有一个想法在函数 create() 中创建 if else 语句,但我不知道什么是正确的查询。 我已经尝试过的:

public function create()
  {
     $expired = Buyer::select('id')->where('expired_at','>', now())->get();
     $valid = Buyer::select('id')->where('expired_at','<', now())->get();
     if ($expired) {
         return view('buyers.complaints.create');
     }
     elseif ($valid) {
         return view('buyers.complaints.expired');
     }
  }

我不知道我要做什么,因为没有区别。 如果买家的责任期已经结束(过期日期已经过了今天的日期)也可以查看买家\\投诉\\create.blade.php并且我还是 Laravel 的新手。 所以,我希望有人可以帮助我解决这个问题。 先谢谢了。

您只有 2 个结果(除非空白响应是第 3 个),因此检查记录是否存在的查询应该可以工作:

if (Buyer::where('expired_at', '>=', now())->exists()) {
    return view('buyers.complaints.create');
}

return view('buyers.complaints.expired');

这是假设您正在寻找任何尚未过期的买家。

如果您想将其限制为某个买家并将买家传递给视图:

$buyer = Buyer::findOrFail($buyerId);

$view = ($buyer->expired_at >= now()) ? 'create' : 'expired';

return view('buyers.complaints.'. $view, ['buyer' => $buyer]);

暂无
暂无

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

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