使用Blade

时间:2016-04-08 00:27:31

标签: php laravel blade

我现在就是这样做的:

<p><strong>@lang('date.monday')</strong>    <span>{{ $profile->getSchedule()[\Carbon\Carbon::MONDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::MONDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.tuesday')</strong>   <span>{{ $profile->getSchedule()[\Carbon\Carbon::TUESDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::TUESDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.wednesday')</strong> <span>{{ $profile->getSchedule()[\Carbon\Carbon::WEDNESDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::WEDNESDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.thursday')</strong>  <span>{{ $profile->getSchedule()[\Carbon\Carbon::THURSDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::THURSDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.friday')</strong>    <span>{{ $profile->getSchedule()[\Carbon\Carbon::FRIDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::FRIDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.saturday')</strong>  <span>{{ $profile->getSchedule()[\Carbon\Carbon::SATURDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::SATURDAY]['to'] }}:00</span></p>
<p><strong>@lang('date.sunday')</strong>    <span>{{ $profile->getSchedule()[\Carbon\Carbon::SUNDAY]['from'] }}:00 - {{ $profile->getSchedule()[\Carbon\Carbon::SUNDAY]['to'] }}:00</span></p>

存储为JSON并转换为数组的计划,即$profile->getSchedule()将此结构作为数组返回。 例如:

{
  "0" : {
    "from" : "05",
    "to" : "15"
  },
  "1" : {
    "from" : "14",
    "to" : "08"
  },
  "2" : {
    "from" : "10",
    "to" : "04"
  },
  "3" : {
    "from" : "11",
    "to" : "00"
  },
  "4" : {
    "from" : "21",
    "to" : "19"
  },
  "5" : {
    "from" : "02",
    "to" : "20"
  },
  "6" : {
    "from" : "13",
    "to" : "20"
  }
}

如果您还可以提出更好的日程安排选项(以简化渲染),我将很高兴听到它们。

1 个答案:

答案 0 :(得分:0)

您应该只在控制器中处理所有这些逻辑。在您的控制器中,运行$profile->getSchedule()一次,然后执行Carbon解析并将$schedule发送到您的视图。

$schedule可能看起来像

[
  Monday:{
    open:'08:00',
    close: '22:00'
  }
  ...
];

然后在刀片中你正在做:

@foreach($schedule as $day => $hours)

  <p><strong>{{$day}}</strong>
  <span>{{ $hours['open'] }} - {{ $hours['close'] }}</span></p>

@endforeach