Use findOrFail to make sure you get a record
$product = Product::findOrFail($id);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to delete products in bulk but I'm getting this error:
Call to a member function related() on null
but Single item delete is working.
Delete Method:
public function destroy(Request $request, $id)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
//delete related items
$product = Product::where('id', $id)->first();
$product->related()->sync($request->related_id);
// Init array of IDs
$ids = [];
if (empty($id)) {
// Bulk delete, get IDs from POST
$ids = explode(',', $request->ids);
} else {
// Single item delete, get ID from URL
$ids[] = $id;
}
foreach ($ids as $id) {
$data = call_user_func([$dataType->model_name, 'findOrFail'], $id);
// Check permission
$this->authorize('delete', $data);
$model = app($dataType->model_name);
if (!($model && in_array(SoftDeletes::class, class_uses_recursive($model)))) {
$this->cleanup($dataType, $data);
}
}
$displayName = count($ids) > 1 ? $dataType->getTranslatedAttribute('display_name_plural') : $dataType->getTranslatedAttribute('display_name_singular');
$res = $data->destroy($ids);
return redirect()->back()->with($data);
}
@Sharim Weird that should work! I dont understand why you are getting that error. Can you try the old way?
$products = Product::whereIn('id', $ids)->get();
foreach ($products as $product) {
$product->related()->sync($request->related_id);
}
Please or to participate in this conversation.