dimanche 23 septembre 2018

How to check existance of category name and parent ID in database using PHP?

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:

  1. Category name
  2. Category slug
  3. Parent ID of an already existing category
  4. Category description

I need to check if there is already a category with the same

  1. Category name
  2. 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