clone the content to new variable ?
protected function prepareForValidation()
{
$this->merge([
'items' => $this->getContent(),
]);
}
public function rules(): array
{
dd($this->items);
...
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Following on from my question earlier, https://laracasts.com/discuss/channels/laravel/best-practice-for-crud-updating-an-existing-belongstomany-in-api
I have now decided on the following structure for my endpoint
curl --request PATCH \
"http://connect.lndo.site/api/custom_forms/crud/bff1f8e1-bccf-61e8-4ed4-9c53130e9d51/relationships/items" \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--data "[
{
\"type\": \"items\",
\"id\": \"bff1f8e1-bccf-61e8-4ed4-9c53130e9d52\"
},
{
\"type\": \"items\",
\"id\": \"bff1f8e1-bccf-61e8-4ed4-9c53130e9d51\"
}
]"
The question I've got now, is how would I go about validating the payload I'm passing to ensure id's are unique and we have a type being passed in too?
I've tried the following:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'*' => 'array',
'*.type' => 'required|string',
'*.id' => 'required|string'
];
}
but I'm not getting the 422 response I was expecting when passing in nothing in my body.
Documents suggest adding a key
return [
'items.*.type' => 'required|string',
'items.*.id' => 'required|string'
];
but it feels like I should be able to do this without modifying the request body.
Please or to participate in this conversation.