I want to check the existence of a category in the database with the same name and same parent ID received as parameters from a form to create a new category.
So basically, Parameters received from form:
- Category name
- Category slug
- Parent ID of an already existing category
- Category description
I need to check if there is already a category with the same
- Category name
- Parent ID
I've already written a function for it but it doesn't seem to work.
foreach(Category::all() as $c) {
if($c->name === $request->name) {
if($c->parent_id === $request->parent_id) {
return redirect(route('admin.products.categories'))->with([
'error' => 'A term with the name provided already exists.'
]);
} else {
break;
}
} else {
break;
}
}
I'm a noob at Laravel and PHP so I think I'm having trouble properly breaking out of the foreach loop.
Appreciate the help and please don't be brutal with your reply if you find a similar question to mine here at Stack overflow. I couldn't find any.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire