Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BladeRunner's avatar

Checking if unique comination of fields exist in database

I have author(name, age and address) and books(title and release date) tables in my database.

I have to fill the tables through one form.

Before making database entry I need to check if unique combination of author exists (if it has the same name, address and age I should not make another entry, but just get an ID) and if the book with the same title and rlease date exists.

How do I make those checks?

0 likes
11 replies
BladeRunner's avatar

This is the code:

public function store(Request $request) { $this->validate($request, [

        'authorName'=>'required|min:3',
        'authorAge'=>'required|numeric|min:16|max:120',
        'authorAddress'=>'required|min:3',
        'bookName'=>'required|min:2',
        'date'=>'required|date',

    ]);


  

    if (!$author = Author::where('name', 'LIKE', '%'.$request->authorName.'%')->where('age', '=', $request->authorAge)->where('address', 'LIKE', '%'.$request->authorAddress.'%')->get()) {

        $author = new Author;
        $author->name = $request->authorName;
        $author->age = $request->authorAge;
        $author->address = $request->authorAddress;
        $author->save();
        dd($author);


    } 

    if (!$book = Book::where('name', 'LIKE', '%'.$request->bookName.'%')->where('release_date', 'LIKE', '%'.$request->date.'%')->get()) {
        
        $book = new Book;
        $book->name = $request->bookName;
        $book->release_date = $request->date;
        $book->author_id = $author->id;
        $book->save();

    }else{

        return redirect()->back()->with('alreadyexistsMsg','Book already exists');

    }

   
   return redirect()->back()->with('successMsg','Author and Book have been saved');

}
tykus's avatar
tykus
Best Answer
Level 104

Firstly, get() returns a Collection; so even when empty, it will be truthy meaning you will never create the new Author. There is a firstOrCreate() method available which should solve this issue for you:

Author::firstOrCreate([
    'name' => $request->authorName,
    'age' => $request->authorAge,
    'address' => $request->authorAddress,
]);

Note that this checks for equality of the request params, which should be the test if you're looking for uniqueness of the entries.

BladeRunner's avatar

is this for the uniqueness of combination of entries?

tykus's avatar

Correct, it is a where ... and ... and ...

BladeRunner's avatar

Is there an error message sent to view in case that there is a existing item in database?

tykus's avatar

No. Eloquent will retrieve the first match or create a new one. You can check if the instance is new using the wasRecentlyCreated property on the result and flash a message to the next request if you wish, e.g.

$author = Author::firstOrCreate([
    //
]);

if (!$author->wasRecentlyCreated) {
    session()->flash('author', 'An existing author was found');
}
1 like
AddWebContribution's avatar

Please update your validation with unique field.

public function store(Request $request) { 
    $this->validate($request, [

        'authorName'=>'required|min:3|unique:author',
        'authorAge'=>'required|numeric|min:16|max:120|unique:author',
        'authorAddress'=>'required|min:3|unique:author',
        'bookName'=>'required|min:2',
        'date'=>'required|date',

    ]);
---------------------
---------------------
}
tykus's avatar

In fact, the unique validation should be removed since you want to get the existing if it exists, not throw a Validation Exception.

1 like

Please or to participate in this conversation.