[Vuejs]-Find duplicates in Laravel Eloquent

1👍

You can do it using updateOrCreate method, the first array is the unique values that you are looking for, if not found it will create the entry, if it finds them it will just update the fields in the second array so do this instead:

public function add(Request $request){

    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required',     
    ]);

    $researcherToProject = ProjectResearchers::updateOrCreate(
        ['user_id' => $request->userSelected, 'project_id' => $request->projectSelected],
        ['created_at' => Carbon::now(), 'updated_at' => Carbon::now()] 
    );

    return new ProjectsResearchersResource($researcherToProject);
}

or if you don’t want to update at all, you can just check if it exists before storing:

public function add(Request $request){

    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required',     
    ]);

       $researcherToProject = new ProjectResearchers(); 

    if( ! ProjectResearchers::where('user_id', $request->userSelected)->where('project_id', $request->projectSelected)->exists()) {
       $researcherToProject = new ProjectResearchers(); 
       $researcherToProject->user_id = $request->userSelected;
       $researcherToProject->project_id = $request->projectSelected;
       $researcherToProject->created_at = Carbon::now();
       $researcherToProject->updated_at = Carbon::now();

       $researcherToProject->save();
    } else {
        $researcherToProject = ProjectResearchers::where('user_id', $request->userSelected)->where('project_id', $request->projectSelected)->first(); 
    }

    return new ProjectsResearchersResource($researcherToProject);
}

0👍

Additionaly to @nakov answer:

firstOrCreate() can combine 2 ways and looks cleaner:

public function add(Request $request){
    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required',     
    ]);

    $researcherToProject = ProjectResearchers::firstOrCreate([
        'user_id' => $request->userSelected,
        'project_id' => $request->projectSelected
    ]); 

    $researcherToProject->created_at = Carbon::now();
    $researcherToProject->updated_at = Carbon::now();
    $researcherToProject->save();

    return new ProjectsResearchersResource($researcherToProject);
}

Or if you don’t want to update:

public function add(Request $request){
    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required',     
    ]);

    $researcherToProject = ProjectResearchers::firstOrCreate([
        'user_id' => $request->userSelected,
        'project_id' => $request->projectSelected
    ]);

    if(!$researcherToProject->id){
        $researcherToProject->created_at = Carbon::now();
        $researcherToProject->updated_at = Carbon::now();
        $researcherToProject->save();
    }

    return new ProjectsResearchersResource($researcherToProject);
}

-1👍

change the following:

    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required',     
    ]);

to

    $request->validate([
        'userSelected' => 'required', 
        'projectSelected' => 'required|unique:ProjectResearchers,project_id,NULL,id,user_id,'.$request->userSelected   
    ]);

this will work check if the combination of user-project already exists inthe table called ProjectResearchers.
for more information about the unique validation rule visit the Laravel Documentation.

Leave a comment