Kalim
Kalim

Reputation: 499

Laravel addOrUpdate() on basis of related table values?

Below is something I am trying to do:

I have users table and user_profiles table and I am inserting name, email in users table and phone, address, .... etc in user_profiles table.

I have to match each value to prevent duplication of user, I have found this laravel method addOrUpdate() but it works only for one table. But I have to match user_profiles values too i.e phone,address.

Below is example code

                $result = $customer->updateOrCreate([
                    'name' => $request->name,
                    'city_id' => $request->city_id,
                    'area_id' => $request->area_id,
                    'email' => $request->email
                ], [
                    'name' => $request->name,
                    'city_id' => $request->city_id,
                    'area_id' => $request->area_id,
                    'email' => $request->email
                ]);

There any way to achieve this using Laravel techniques?

Regards

Upvotes: 1

Views: 874

Answers (4)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You could use updateOrCreate for both of your models for sake of uniqueness i assume email should be unique so first updateOrCreate() method will check if user exists for parameter $request->email then update if not then create and return user model instance (in both update/create case)

Second updateOrCreate() on UserProfile will check if there exist any data for user_id then update else add new row, I assume user_id will be a foreign key in user profile table

$user = User::updateOrCreate([
                    'email' => $request->email
                ], [
                    'name' => $request->name,
                    'email' => $request->email
                ]);
UserProfile::updateOrCreate([
                    'user_id' => $user->id
                ], [
                    'user_id' => $user->id,
                    'city_id' => $request->city_id,
                    'area_id' => $request->area_id
                ]);

Upvotes: 0

Sohel0415
Sohel0415

Reputation: 9853

First make a relationship with user and user_profiles model like-

public function userProfile()
{
    return $this->hasOne('App\Model\UserProfile','user_id','id');
}

And then in your post controller as you want to match each value to prevent duplication of user-

$result = User::where('name',$request->name)->where('email',$request->email)
       ->whereHas('userProfile', function($q) use ($request){
            $q->where('city_id'$request->city_id)->where('area_id',$request->area_id)

  )->first();
if(!$result){
     ////your registration here
}

Upvotes: 2

skribe
skribe

Reputation: 3615

I would recommend using Laravel's Validator Facade. https://laravel.com/docs/5.4/validation#manually-creating-validators

This would check to make sure the name and email fields of the users and users_profile table are unique.

    $validator = Validator::make($request->all(), [
        'name' => 'required|unique:users|unique:users_profile',
        'email' => 'required|unique:users|unique:users_profile',
    ]);

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

If you want to check if a user with exactly the same data exists, you can't use updateOrCreate(). Do this instead:

$user = User::where('name', $request->name)->where('email', $request->email)->first();
$profile = $user->profile()->where('phone', $request->phone)->where('address', $request->address)->count();
if ($user && $profile) {
    // User exists.
}

Upvotes: 0

Related Questions