Home Laravel addOrUpdate() on basis of related table values?
Reply: 4

Laravel addOrUpdate() on basis of related table values?

Kaleem Published in 2018-01-11 07:37:18Z

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?


Sohel0415 Reply to 2018-01-11 07:53:19Z

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){

     ////your registration here
Alexey Mezenin
Alexey Mezenin Reply to 2018-01-11 07:41:59Z

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.
skribe Reply to 2018-01-11 07:48:38Z

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',
M Khalid Junaid
M Khalid Junaid Reply to 2018-01-11 07:54:12Z

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
                    'user_id' => $user->id
                ], [
                    'user_id' => $user->id,
                    'city_id' => $request->city_id,
                    'area_id' => $request->area_id
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315366 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO