Я пытался поместить несколько массивов [] в свои имена и переменные контроллера, но это не сработало.
Вот моя форма:
Код: Выделить всё
Set Appointment Details
@if($bookingRequest->appointment)
{!! Form::model($bookingRequest->appointment, ['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
@else
{!! Form::open(['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
@endif
Session
{!! Form::inputGroup('date', 'Starting', 'start_date[]') !!}
{!! Form::inputGroup('date', 'Until', 'end_date[]') !!}
{!! Form::inputGroup('time', ' ', 'start_date_time[]') !!}
{!! Form::inputGroup('time', ' ', 'end_date_time[]') !!}
{!! Form::inputGroup('text', 'Other Services Applied', 'other_services[]') !!}
{!! Form::inputGroup('number', 'Fee', 'other_services_fee[]') !!}
[i][/i]
[i][/i] Add new session
Submit
{!! Form::close() !!}
@endif
Код: Выделить всё
$(document).ready(function(){
var postURL = "";
var table = $(this).closest('table.dynamic');
var i = 1;
$('#add-line').click(function (){
i++;
$('#dynamic').append(
''+
'Add another session'+
''+
''+
''+
'{!! Form::inputGroup("date", "Starting", "start_date[]") !!}' +
'{!! Form::inputGroup("date", "Until", "end_date[]") !!}'+
'' +
''+
''+
'{!! Form::inputGroup("time", "Start Time", "start_date_time[]") !!}' +
'{!! Form::inputGroup("time", "End Time", "end_date_time[]") !!}'+
'' +
''+
''+
'{!! Form::inputGroup("text", "Other Services Applied", "other_services[]") !!}' +
'{!! Form::inputGroup("number", "Fee", "other_services_fee[]") !!}'+
''
);
});
$(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',
});
});
});
Код: Выделить всё
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');
}
!!ОБНОВЛЕНИЕ!!
Я теперь использую этот код в своем контроллере
Код: Выделить всё
$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}");
}
}

Подробнее здесь: https://stackoverflow.com/questions/557 ... base-error