Laravel,jQuery:CALENDAR DYNAMIC FORM插入数据库错误

问题描述 投票:0回答:1

我有一个动态表单,使用户可以输入属于单个ID的多个日期。我目前在删除行按钮和数据库插入时遇到问题。我需要帮助。

我尝试在我的名字和控制器变量中放置一些数组[],但它没有用。

这是我的表格:

<div class="card">
                <div class="card-header bg-info">
                    Set Appointment Details
                </div>
                <div class="card-body"  style="overflow: scroll;height: 605px;">
                        @if($bookingRequest->appointment)
                            {!! Form::model($bookingRequest->appointment, ['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
                        @else
                            {!! Form::open(['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
                        @endif  
                    <table class="table dynamic" id="dynamic">
                        <thead>
                            <th>
                                Session
                            </th>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <div class="form-row data">
                                        <div class="col-6">
                                            {!! Form::inputGroup('date', 'Starting', 'start_date[]') !!}
                                        </div>
                                        <div class="col-6">
                                            {!! Form::inputGroup('date', 'Until', 'end_date[]') !!}
                                        </div>
                                    </div>
                                </td>

                            </tr>
                            <tr>
                                <td>
                                    <div class="form-row data">
                                        <div class="col-6">
                                            {!! Form::inputGroup('time', '&nbsp;', 'start_date_time[]') !!}
                                        </div>
                                        <div class="col-6">
                                            {!! Form::inputGroup('time', '&nbsp;', 'end_date_time[]') !!}
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    {!! Form::inputGroup('text', 'Other Services Applied', 'other_services[]') !!}
                                    {!! Form::inputGroup('number', 'Fee', 'other_services_fee[]') !!}
                                </td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <td>
                                <div class="form-row form-group data">
                                    <div class="col-8">
                                        <button class="btn btn-sm btn-danger remove-line" id="remove-line"><i class="fa fa-times"></i></button>
                                        <button type="button" class="btn btn-sm btn-outline-primary add-line" id="add-line">
                                        <i class="fa fa-plus"></i> Add new session</button>
                                    </div>
                                </div>
                            </td>
                        </tfoot>
                    </table>






                        <button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button>
                    {!! Form::close() !!}
                </div>
            </div>
            @endif
        </div>

这是我的脚本:

<script type="text/javascript">
    $(document).ready(function(){

        var postURL = "<?php echo url('/therapist-calendar')?>";
        var table = $(this).closest('table.dynamic');
        var i = 1;

        $('#add-line').click(function (){
            i++;

            $('#dynamic').append(
                '<tr id="row'+i+'" class="dynamic-added">'+
                '<th>Add another session</th>'+
                '</tr>'+
                ''+
                '<tr id="row'+i+'" class="dynamic-added">'+
                '<td><div class="form-row data"><div class="col-6">{!! Form::inputGroup("date", "Starting", "start_date[]") !!}</div>' +
                '<div class="col-6">{!! Form::inputGroup("date", "Until", "end_date[]") !!}</div></div></td>'+
                '</tr>' +
                ''+
                '<tr id="row'+i+'" class="dynamic-added">'+
                '<td><div class="form-row data"><div class="col-6">{!! Form::inputGroup("time", "Start Time", "start_date_time[]") !!}</div>' +
                '<div class="col-6">{!! Form::inputGroup("time", "End Time", "end_date_time[]") !!}</div></div></td>'+
                '</tr>' +
                ''+
                '<tr id="row'+i+'" class="dynamic-added">'+
                '<td>{!! Form::inputGroup("text", "Other Services Applied", "other_services[]") !!}' +
                '{!! Form::inputGroup("number", "Fee", "other_services_fee[]") !!}</td>'+
                '</tr>' 

            );
        });

        $(document).on('click', '.remove-line', function (){

            var button_id = $(this).attr("id");

            $('#row'+button_id+'').remove();
        });

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

        $('submit').click(function(){
            $.ajax({
                URL: postURL,
                method: "POST",
                data: $('#add_name').serialize(),
                type: 'json',
            });
        });
    });
    </script>

这是我的控制器:

public function saveAppointment(Request $request, BookingRequest $bookingRequest)
    {
        $validator = \Validator::make($request->all(), [
            'start_date'         => 'required|date|after:yesterday',
            'end_date'           => 'required|date|after_or_equal:start_date',
            'start_date_time'    => 'required|date_format:H:i',
            'end_date_time'      => 'required|date_format:H:i',
            'other_services'     => 'sometimes|nullable|string',
            'other_services_fee' => 'sometimes|nullable|numeric',
        ]);

        $bookingRequest->load('appointment');
        Auth::user()->load([
            'therapist.appointments[]'
        ]);

        $validator->after(function ($v) use ($request, $bookingRequest) {
            if ($v->errors()) {
                $startDateTime[] = Carbon::parse("{$request->start_date} {$request->start_date_time}");
                $endDateTime[]   = Carbon::parse("{$request->end_date} {$request->end_date_time}");

                /** @NOTE: lte => Less than or equal */
                if ($endDateTime->lte($startDateTime)) {
                    $v->errors()->add('end_date_time', 'This should be after start date and time');

                    return;
                }

                $conflicts = data_get(Auth::user(), 'therapist.approvedAppointments')
                    ->filter(function ($existingAppointment) use ($startDateTime, $bookingRequest) {
                        // ignore the same appointments
                        if(optional($bookingRequest->appointment)->id === $existingAppointment->id){
                            return false;
                        }

                        return $startDateTime->between($existingAppointment->start_timestamp, $existingAppointment->end_timestamp);
                    });

                if($conflicts->isNotEmpty()){
                    $v->errors()->add('start_date', 'Conflict');
                    $v->errors()->add('end_date', 'Conflict');
                    $v->errors()->add('end_date_time', 'Conflict');
                    $v->errors()->add('start_date_time', 'Conflict');
                }
            }
        });

        $input = $validator->validate();

        $appointment = array_merge(
            $input,
            $bookingRequest->only(['client_id', 'therapist_id']),
            ['name' => '-', 'address' => '-']
        );

        \DB::transaction(function () use ($bookingRequest, $appointment) {
            if ($bookingRequest->appointment()->exists()) {
                $bookingRequest->appointment()->update($appointment);
            } else {
                $bookingRequest->approve();
                $bookingRequest->appointment()->create($appointment);
            }
        });

        return redirect()->back()->with('message', 'Booking successful');
    }

当我单击我的删除行按钮时,出现Array to string转换错误。我也很难将其插入数据库。我需要帮助。

!! UPDATE!

我现在在我的控制器中使用此代码

$request = $request->all();
            $input = array();

            foreach($request['start_date'] as $key => $startDate){
                foreach($request['start_date_time'] as $key => $startDateTime){

                    $input[$key]['start_date'] = $startDate;
                    $input[$key]['start_date_time'] = $startDateTime;

                    $dateTimeDuration = Carbon::parse("{$startDate} {$startDateTime}");
                }
            }

但我似乎在提交时收到此错误

enter image description here

我的添加视图按钮工作正常,而我的删除按钮不起作用。

enter image description here

jquery laravel laravel-5 fullcalendar laravel-5.6
1个回答
0
投票

要根据您的代码验证laravel中的数组,您需要这样做

$validator = Validator::make($request->all(), [
        'start_date'  => 'required|array|date|after:yesterday', //OR
        "start_date.*"  => => 'required|date|after:yesterday'
]);

星号(*)表示您要检查数组中的VALUES

接下来是

在删除链接按钮中添加type =“Button”。否则它会在单击时提交您的表单。对于大多数浏览器,默认类型的按钮是提交。

© www.soinside.com 2019 - 2024. All rights reserved.