I am sending these objects to server to fetch the record and update it. But it is giving me the below given error.
Wnen I add dd
, I see this message.
FilterController.php
public function update(Request $request, Filter $filter)
{
$filters = collect($request->filters);
dd($filters);
$filters->each(function ($item) use ($request, $filter) {
if (isset($item['name']) && isset($item['latin'])) {
$data = [
'category_id' => $request->category_id,
'name' => $item['name'],
'latin' => $item['latin'],
'field' => $item['field'],
];
$filter_item = Filter::query()->firstOrCreate(['id' => $item['id'] ?? null]);
$filter_item->fill($data)->save();
} else {
return null;
}
if (isset($item['value'])) {
foreach($item['value'] as $value) {
$filter_item->values()->updateOrCreate(
['id' => $value['id']],
['value' => $value['value']]
);
}
}
});
return redirect()->route('filters.index');
}
CodePudding user response:
as per your screen short you don't have $value['id']
it will be like
if (isset($item['value'])) {
foreach($item['value'] as $key => $value) {
$filter_item->values()->updateOrCreate(
['id' => $key],
['value' => $value]
);
}
}
as $item['value']
is normal array with only index