Ad

How To Convert This Type Code Into Laravel Form?

- 1 answer

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);
}

This code is about the normal php code, i want to put into laravel controller but it doesnt work. My question is want to ask about how to make this code function and link with my database.

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

This is my view code, could anyone help me and show me how to link with it? So i can fetch the data from my database and display it on my fullcalendar.


After edited.. This is my View code.

    $(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>

This is my EventsController

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

    }
Ad

Answer

First of all as you are using POST method & I suppose you are csrf middleware globally so whenever you will request POST method you will have to pass csrf token along with your request. To do that you can add csrf token to meta

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

& in your jQuery code

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

You can check more details https://laravel.com/docs/5.1/routing#csrf-protection

Now in your jQuery code

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

Now in your controller action

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
    }
}

You can check https://laravel.com/docs/5.1/eloquent#defining-models here on how to define model.

Ad
source: stackoverflow.com
Ad