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

sanainfotech's avatar

Query to check if user_id exist in multiple tables

I am having 4 tables in my database,

users

profiledetails has FK user_id

physical has FK user_id

education has FK user_id

Scenario 1

Signup are stored in users table After successfully signed up , They need to go through registration process. Registration process is 3 different form for 3 different table as mention above.

Scenario 2

User has logged in, who is already signed up user I would like to check weather he has filled up all the form or not, if not redirect to specific form,

Hope make sense.

0 likes
3 replies
bobbybouwmann's avatar

You can simply check if the relation between the items exists. Note that this only works if you have setup the relations correctly

$user = Auth::user();

$user->load(['profiledetails', 'physical', 'education']);

if (! $user->education) {
    // redirect to education form
}

if (! $user->physical) {
    // redirect to physical form
}

// And so on.

If there is no relation set yet it will return null so you can check for that

1 like
sanainfotech's avatar

@boobybouwmann Thanks, yes I have relation between the tables setup,

In AuthController

protected $redirectTo = '/profiledetails/create';

In What controller do I need to add your mentioned code

sanainfotech's avatar

@boobybouwmann

Hi , when I am in current form, for ex:-profiledetails

Before I go to next form I would like to check if relation exist,

Is the below code right?

 public function store(CreatingProfileRequest $request)
  {
     //Profiledetails::create($request->all()); // with out relation
    $input =   new Profiledetails($request->all());
    
    Auth::user()->profiledetailsHasOne()->save($input);      
    $this->formCheck();
  }

public function formCheck(){      
       $user = Auth::user();       
       $user->load(['profiledetailsHasOne','educationHasOne','occupationHasOne','maritalHasOne','parentsHasOne','addressHasOne']);
       
   if (! $user->educationHasOne) {
            return redirect('parents/create');
        }
        elseif (! $user->occupationHasOne) {
            return redirect('occupation/create');
        }
        elseif (! $user->maritalHasOne) {
            return redirect('marital/create');
        } 
        elseif (! $user->parentsHasOne) {
            return redirect('parents/create');
        }
        elseif (! $user->addressHasOne) {return redirect('address/create');}            
        else{ return redirect('profile');}      
    }

Please or to participate in this conversation.