fikri1510
9 months ago
50
10
Laravel

How to assign field from another model?

Posted 9 months ago by fikri1510

I have Model Profile and Participant, they are related by ssn. I want to save Participant to database from ParticipantController where one field is coming from Profile model(which is ssn).

public function store(Request $request){
    
    $participant = new Participant;
    $profile = new Profile;   
    $participant->ssn = $profile->ssn; //can I just do like this?
    ...
    $participant->save();  
    
    return view('after_registration');
}

or do i need to write query to retrieve ssn from profile and later assign it to participant field?

Please sign in or create an account to participate in this conversation.