Anonymous
Как проверить массив дат в Laravel
Сообщение
Anonymous » 30 окт 2024, 07:12
Приведенная ниже проверка не проходит для полей start_date и end_date.
ErrorException: strtotime() ожидает, что параметр 1 будет строкой, а массив
задан
Код: Выделить всё
public function rules()
{
return [
'location_id.*' => 'required_without_all:user_id,service_id|integer|gt:0',
'service_id.*' => 'required_without_all:user_id,location_id|integer|gt:0',
'user_id.*' => 'required_without_all:location_id,service_id|integer|gt:0',
'start_date.*' => 'required|date|after_or_equal:now',
'end_date.*' => 'nullable|date|after_or_equal:start_date',
'day_name.*' => 'nullable|integer|between:1,7',
'start_time.*' => 'required|date_format:H:i|before:end_time',
'end_time.*' => 'required|date_format:H:i|after:start_time',
'created_by_user_id.*' => 'required|integer|gt:0',
];
}
protected function prepareForValidation()
{
$row = count($this->day_name);
$this->merge([
'location_id' => array_fill(0, $row, $this->location_id),
'service_id' => array_fill(0, $row, $this->service_id),
'user_id' => array_fill(0, $row, $this->user_id),
'start_date' => array_fill(0, $row, Carbon::parse($this->start_date)),
'end_date' => array_fill(0, $row, Carbon::parse($this->end_date)),
'day_name' => array_fill(0, $row, $this->day_name),
'start_time' => array_fill(0, $row, $this->start_time),
'end_time' => array_fill(0, $row, $this->end_time),
'created_by_user_id' => array_fill(0, 3, $this->created_by_user_id),
]);
Начальная дата — это экземпляр углерода, как показано ниже:
Код: Выделить всё
dd($this->start_date)); // In the start of public function rules()
array:3 [
0 => Carbon\Carbon @1587484783^ {#1470
#constructedObjectId: "000000006789d240000000000f12e56f"
#localMonthsOverflow: null
#localYearsOverflow: null
#localStrictModeEnabled: null
#localHumanDiffOptions: null
#localToStringFormat: null
#localSerializer: null
#localMacros: null
#localGenericMacros: null
#localFormatFunction: null
#localTranslator: null
#dumpProperties: array:3 [
0 => "date"
1 => "timezone_type"
2 => "timezone"
]
#dumpLocale: null
date: 2020-04-21 15:59:43.430962 UTC (+00:00)
timezone: "UTC"
}
1 => Carbon\Carbon @1587484783^ {#1470}
2 => Carbon\Carbon @1587484783^ {#1470}
]
Все поля принадлежат одной таблице. Некоторые из них не были массивами, а некоторые были входными массивами. Поэтому преобразовал их все в массивы для вставки нескольких записей.
Подробнее здесь:
https://stackoverflow.com/questions/613 ... in-laravel
1730261577
Anonymous
Приведенная ниже проверка не проходит для полей start_date и end_date. ErrorException: strtotime() ожидает, что параметр 1 будет строкой, а массив задан [code]public function rules() { return [ 'location_id.*' => 'required_without_all:user_id,service_id|integer|gt:0', 'service_id.*' => 'required_without_all:user_id,location_id|integer|gt:0', 'user_id.*' => 'required_without_all:location_id,service_id|integer|gt:0', 'start_date.*' => 'required|date|after_or_equal:now', 'end_date.*' => 'nullable|date|after_or_equal:start_date', 'day_name.*' => 'nullable|integer|between:1,7', 'start_time.*' => 'required|date_format:H:i|before:end_time', 'end_time.*' => 'required|date_format:H:i|after:start_time', 'created_by_user_id.*' => 'required|integer|gt:0', ]; } protected function prepareForValidation() { $row = count($this->day_name); $this->merge([ 'location_id' => array_fill(0, $row, $this->location_id), 'service_id' => array_fill(0, $row, $this->service_id), 'user_id' => array_fill(0, $row, $this->user_id), 'start_date' => array_fill(0, $row, Carbon::parse($this->start_date)), 'end_date' => array_fill(0, $row, Carbon::parse($this->end_date)), 'day_name' => array_fill(0, $row, $this->day_name), 'start_time' => array_fill(0, $row, $this->start_time), 'end_time' => array_fill(0, $row, $this->end_time), 'created_by_user_id' => array_fill(0, 3, $this->created_by_user_id), ]); [/code] Начальная дата — это экземпляр углерода, как показано ниже: [code]dd($this->start_date)); // In the start of public function rules() array:3 [ 0 => Carbon\Carbon @1587484783^ {#1470 #constructedObjectId: "000000006789d240000000000f12e56f" #localMonthsOverflow: null #localYearsOverflow: null #localStrictModeEnabled: null #localHumanDiffOptions: null #localToStringFormat: null #localSerializer: null #localMacros: null #localGenericMacros: null #localFormatFunction: null #localTranslator: null #dumpProperties: array:3 [ 0 => "date" 1 => "timezone_type" 2 => "timezone" ] #dumpLocale: null date: 2020-04-21 15:59:43.430962 UTC (+00:00) timezone: "UTC" } 1 => Carbon\Carbon @1587484783^ {#1470} 2 => Carbon\Carbon @1587484783^ {#1470} ] [/code] Все поля принадлежат одной таблице. Некоторые из них не были массивами, а некоторые были входными массивами. Поэтому преобразовал их все в массивы для вставки нескольких записей. Подробнее здесь: [url]https://stackoverflow.com/questions/61348155/how-to-validate-an-array-of-dates-in-laravel[/url]