Я работаю над Dynami c Форма ввода в Laravel -5,8:
public function store(StoreGoalRequest $request)
{
$goal = new Goal();
$goal->goal_type_id = $request->goal_type_id;
$goal->save();
foreach(Arr::wrap($request->activity) as $key => $activity){
$startDate = Carbon::parse($request->start_date[$key]);
$endDate = Carbon::parse($request->end_date[$key]);
$goaldetail = new GoalDetail();
$goaldetail->activity = $request->activity[$key];
$goaldetail->start_date = $startDate->toDateString();
$goaldetail->end_date = $endDate->toDateString();
$goaldetail->save();
}
}
Правила
'start_date.*' => [
'required',
'date',
'date_format:d-m-Y',
],
'end_date.*' => [
'required',
'date',
'date_format:d-m-Y',
'after:start_date.*'
],
модель
protected $dates = [
'start_date',
'end_date'
];
просмотр
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Start Date<span style="color:red;">*</span></th>
<th scope="col">End Date<span style="color:red;">*</span></th>
<th scope="col" width="8%"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
<td>
<input id="startDate" type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="start_date[]" class="form-control">
</td>
<td>
<input id="endDate" type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="end_date[]" class="form-control">
</td>
</tbody>
</thead>
</table>
<script src="{{ asset('theme/adminlte3/plugins/jquery/jquery.js') }}"></script>
<!-- jQuery UI 1.11.4 -->
<script src="{{ asset('theme/adminlte3/plugins/jquery-ui/jquery-ui.js') }}"></script>
<script type="text/javascript">
$(function () {
$( "#startDate" ).datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showAnim: 'slideDown',
duration: 'fast',
yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
});
$( "#endDate" ).datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showAnim: 'slideDown',
duration: 'fast',
yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
var isHod = {{ Auth::user()->is_hod == 0 ? 0 : 1 }};
var numRows = $('.activity').length
if (numRows<4) {
addRow();
$('.startDate').datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showAnim: 'slideDown',
duration: 'fast',
yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
});
$('.endDate').datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
showAnim: 'slideDown',
duration: 'fast',
yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
});
}
});
function addRow() {
var addRow = '<tr>\n' +
' <td><div class="input-group"><div class="input-group-prepend"><span class="input-group-text"><i class="far fa-calendar-alt"></i></span></div><input type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="start_date[]" class="form-control startDate"></div></td>\n' +
' <td><div class="input-group"><div class="input-group-prepend"><span class="input-group-text"><i class="far fa-calendar-alt"></i></span></div><input type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="end_date[]" class="form-control endDate"></div></td>\n' +
' <td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
' </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
function addRemoveListener() {
$('.remove').on('click', function () {
var l =$('tbody tr').length;
if(l==1){
alert('you cant delete last one')
}else{
$(this).parent().parent().remove();
}
});
}
</script>
При отправке я получил следующую ошибку в журнале:
[2020-07-14 11:33:05] production.ERROR: Exception: DateTime :: __ construct ( ): Не удалось проанализировать строку времени (3) в позиции 0 (3): Неожиданный символ в C: \ xampp \ htdocs \ laravelapp \ vendor \ nesbot \ carbon \ src \ Carbon \ Traits \ Creator. php: 81 Трассировка стека: # 0 C: \ xampp \ htdocs \ laravelapp \ vendor \ nesbot \ carbon \ src \ Carbon \ Traits \ Creator. php (81): DateTime -> __ construct ('3', Object (Carbon \ CarbonTimeZone)) # 1 C: \ xampp \ htdocs \ laravelapp \ vendor \ nesbot \ carbon \ src \ Carbon \ Traits \ Creator. php (174): Carbon \ Carbon -> __ construct ('3', NULL) # 2 C: \ xampp \ htdocs \ laravelapp \ vendor \ nesbot \ carbon \ src \ Carbon \ Traits \ Creator. php (201): Carbon \ Carbon :: rawParse ('3', NULL) # 3 C: \ xampp \ htdocs \ laravelapp \ app \ Http \ Controllers \ GoalsController. php (348): Carbon \ Carbon :: parse ('3') # 4 [inte rnal функция]: App \ Http \ Controllers \ GoalsController-> store (Object (App \ Http \ Requests \ Goal \ StoreGoalRequest)) # 5 C: \ xampp \ htdocs \ laravelapp \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Controller. php (54): call_user_func_array (Array, Array) # 6
Как решить эту проблему?
Спасибо