如何将此类型代码转换为拉拉维尔形式


How to convert this type code into laravel form?

if($type == 'fetch')
{
    $events = array();
    $query = mysqli_query($con, "SELECT * FROM calendar");
    while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC))
    {
    $e = array();
    $e['id'] = $fetch['id'];
    $e['title'] = $fetch['title'];
    $e['start'] = $fetch['startdate'];
    $e['end'] = $fetch['enddate'];
    $allday = ($fetch['allDay'] == "true") ? true : false;
    $e['allDay'] = $allday;
    array_push($events, $e);
    }
    echo json_encode($events);
}

这段代码是关于普通的php代码,我想放入laravel控制器,但它不起作用。我的问题是想问一下如何使这段代码起作用并与我的数据库链接。

$.ajax({
        url: 'process.php',
        type: 'POST', // Send post data
        data: 'type=fetch',
        async: false,
        success: function(s){
            json_events = s;
        }
    });

这是我的视图代码,谁能帮助我并告诉我如何链接它?因此,我可以从数据库中获取数据并将其显示在完整日历上。


编辑后..这是我的视图代码。

    $(document).ready(function() {
        $.ajax({
            url: '{{url('events/add')}}',
            type: 'POST', // Send post data
            async: false,
            success: function(s){
                json_events = s;
            }
        });
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            selectable: true,
            selectHelper: true,
            select: function(start, end) {
                var title = prompt('Event Title:');
                var eventData;
                if (title) {
                    eventData = {
                        title: title,
                        start: start,
                        end: end
                    };
                    $('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
                }
                $('#calendar').fullCalendar('unselect');
            },
            editable: true,
            eventLimit: true, // allow "more" link when too many events
        });
    });
</script>

这是我的事件控制器

  public function show(Request $request){
        if($request->get('type') == 'fetch'){
            return Events::select(['calendar_id','calendar_title','startdate as start', 'enddate as end'])->get();
        }
    }
首先,

当你使用POST方法时,我想你是全局csrf中间件,所以每当你请求POST方法时,你都必须csrf令牌与你的请求一起传递。为此,您可以将csrf令牌添加到元

<meta name="csrf-token" content="{{ csrf_token() }}">

&在你的jQuery代码中

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

您可以查看更多详细信息 https://laravel.com/docs/5.1/routing#csrf-protection

现在在你的jQuery代码中

$.ajax({
    url: 'YOUR_URL_TO_POST',
    type: 'POST', // Send post data
    data: 'type=fetch',
    async: false,
    success: function(s){
        json_events = s;
    }
});

现在在您的控制器操作中

public function your_method_name('Request $request)//put your action name & inject Request
{
    if ($request->get('type') == 'fetch')//check type
    {
        return Calendar::select(['id', 'title', 'startdate as start', 'enddate as end', 'allDay'])->get();//I think you have a calendar model
    }
}

您可以在此处查看 https://laravel.com/docs/5.1/eloquent#defining-models 如何定义模型。