I'm pretty much new to opps and laravel both
So, to insert the values into my users
and profiles
table which hav OneToOne
relationship, Here is how my store()
method looks like
public function store(Requests\StoreNewUser $request)
{
// crate an objct of user model
$user = new \App\User;
// now request and assign validated input to array of column names in user table
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->password = $request->input('password');
/* want to assign request input to profile table's columns in one go
*/
$user->profile()->user_id = $user->id; // foreign key in profiles table
$user->profile()->mobile_no = $request->input('mobile');
dd($user); // nothing related to profile is returned
}
I'm creating the new record, hence dd()
never returns anything related to profile table.
Is this Because the $user
object is not including relationship by default?
If yes Can i create the $user
object which includes the associated relations in User
Model ?
Or do i have to create two separate objects of each table and save()
the data
But then what is the significance of push()
method ?
EDIT 1
P.S. yes, the relationships are already defined in User
& Profile
model
You may try something like the following. At first save the parent model like this:
Then create and save the related model using something like this:
Also make sure you have created the
profile
method inUser
model: