Validate in Laravel, That Checkbox IDs are Valid


If you have a form with multiple checkboxes (e.g. categories, tags, …) and you want to validate, that the submitted IDs are valid entries in your database, you can add this validation:

public function store(Request $request)
	$validatedData = $request->validate([
		'category' => 'required', // validates, that at least one checkbox is selected
		'category.*' => 'exists:categories,id', // validates, that a matching database entry is present

	// more code

The ‘category.*’ validator checks every ID in the $request->category array for an matching database entry in the categories table.