如何从数据库中检索数据并在视图页面中按月显示

时间:2016-01-20 17:25:10

标签: php mysql laravel

我有一张名为Reservation

的表
id | property_id | name | checkin_date | checkout_date | guests

1  | 1           | Tim   | 2016-01-10   | 2016-01-15 | 3
2  | 2           | Alex  | 2016-01-16   | 2016-01-26 | 5
3  | 1           | Kevin | 2016-01-28   | 2016-02-10 | 3

等等。

我需要根据月份显示签入日期和结帐日期,如果日期介于两个月之间,结果应显示在两个月...我在视图页面中的预期输出是

January

2016-01-10 To 2016-01-15 Tim Booked
2016-01-16 To 2016-01-26 Alex Booked
2016-01-28 To 2016-01-31 Kevin Booked

February

2016-02-01 To 2016-02-10 Kevin Booked


March, April,June...etc

我已经做了一些部分编码。

在我的控制器中

public function index{

    for($i=1; $i<=12; i++){
        $("reservation".$i)=Reservation::whereMonth('checkin_date','=',$i)
                                        ->get();
    }
    return view('abc',compact(reservation1,reservation2));
}

并在我的视图文件中

<h3>January</h3>
<?php foreach($reservation1 as $january){ ?>
<?php echo $january->checkin_date; ?> To <?php echo $january->checkout_date; ?> <?php echo $january->name; ?> Booked

请有人帮助我获得输出...提前感谢

1 个答案:

答案 0 :(得分:0)

您需要此库FullCalendar

控制器

public function test(){
    $events = [];
    //creating events
    $events[] = \Calendar::event(
        'Angel', //event title
        true, //full day event?
        '2016-01-01', //start time (you can also use Carbon instead of DateTime)
        '2016-01-03', //end time (you can also use Carbon instead of DateTime)
        1, //optionally, you can specify an event ID
        [
            'backgroundColor' => '#ff5722'
        ]
    );
    $events[] = \Calendar::event(
        'Jesus', //event title
        true, //full day event?
        '2016-01-04', //start time (you can also use Carbon instead of DateTime)
        '2016-01-08', //end time (you can also use Carbon instead of DateTime)
        1, //optionally, you can specify an event ID
        [
            'backgroundColor' => '#e97118'
        ]
    );
    //adding events
    $calendar = \Calendar::addEvents($events)->setOptions(['lang' => 'es']);

    return view('pages.calendar')->with('calendar',$calendar);
}

查看(最好使用刀片)

 <div class="row">
    <div class="col-md-12">
        {!! $calendar->calendar() !!}
        {!! $calendar->script() !!}
    </div>
</div>

结果 enter image description here