How to create mutiple things with one resource?

I trid to overwrite create process wthhandleRecordCreation()
function handleRecordCreation(array $data): Model
{
$result = [];
foreach ($data as $key => $value) {
foreach ($value as $d) {
static::getModel()::create($d);
}
}
return $result;
}
function handleRecordCreation(array $data): Model
{
$result = [];
foreach ($data as $key => $value) {
foreach ($value as $d) {
static::getModel()::create($d);
}
}
return $result;
}
but I got the excetion like this
No description
12 Replies
DariusIII
DariusIII4w ago
You are returning an array and not model instance. Why do you use $result at all, if you are not doing anything with it? You always return an empty array.
連城
連城OP4w ago
php
function handleRecordCreation(array $data): Model
{
foreach ($data as $key => $value) {
foreach ($value as $d) {
static::getModel()::create($d);
}
}
}
php
function handleRecordCreation(array $data): Model
{
foreach ($data as $key => $value) {
foreach ($value as $d) {
static::getModel()::create($d);
}
}
}
Ijust remove $result because I got another exception
No description
DariusIII
DariusIII4w ago
Now you are returning nothing. Remove return type declaration.
function handleRecordCreation(array $data)
function handleRecordCreation(array $data)
or set it to : void
But that function needs to return a Model, so my solution will not work Anyway, what are you trying to achieve? When $data is passed to that function it creates all that it needs to be created, no need to alter anything If you need some additional things, you could use afterCreate function
連城
連城OP4w ago
I 'm tring to create mutiple things with one form and Do you have any perfect advice?
DariusIII
DariusIII4w ago
What kind of multiple things? If same model is used, that is done by default based on form data passed
連城
連城OP4w ago
OK it's same model
連城
連城OP4w ago
here is my form and I jus use repeater
DariusIII
DariusIII4w ago
you need JSON column for this, and that is it
DariusIII
DariusIII4w ago
No need to overwrite any function Read the docs, they are really good.
連城
連城OP4w ago
ok let me check docs , Thank you
Want results from more Discord servers?
Add your server