Home  >  Q&A  >  body text

How to insert data in laravel?

Pending:

Table Users
Table UserProfiles

Users has a relationship with UserProfiles

  public function hasOneProfile(){

    return $this->hasOne('App\UserProfile','user_id','id');

  }

UserProfiles has a belongsTo relationship with Users

  public function belongsToUser(){

    return $this->belongsTo('User','user_id','id');

  }

When a new $user = new Users(); is created

How to directly write the attributes of $user so that when save() is achieved, the UserProfile can be created directly?

For example:
Only specify one attribute for UserProfile sex=1 Then how can you directly create it as a new User and UserProfile together...

phpcn_u1582phpcn_u15822700 days ago890

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:57:17

    Create the user profile before saving. The user profile cannot be saved until it is created successfully.

    reply
    0
  • Cancelreply