Reputation: 2736
$user = $this->user;
$user->name = $request['name'];
$user->email = $request['email'];
$user->password = $request['password'];
$user->save();
$name = explode(' ' ,$user->name);
$profile= $user->userdetail()->create([
'user_id' => $request->input('id'),
'first_name' => <the value of the first exploded string>
'last_name' => the value of the secondexploded string
]);
return redirect('confirmation');
}
how to split two words using explode function in php? For example i registered wth name of JOhn doe I want to create in my userdetail table the first_name of john and last_name of doe. How can i do it/
Upvotes: 10
Views: 57955
Reputation: 431
You can use the following code
$full_name = "John Doe";
$name = explode(' ',$full_name);
$first_name = $name[0];
$last_name = $name[1];
Upvotes: 6
Reputation: 91
Alternate way :
<?php
$ip = "fname lname";
$iparr = preg_split("/[\s,]+/",$ip);
echo "$iparr[0]";
echo "$iparr[1]";
?>
But explode is good(faster) than preg_split. ;)
Upvotes: 0
Reputation: 163748
explode()
returns an array of strings, so you can access elements by using keys:
$profile = $user->userdetail()->create([
'user_id' => $request->input('id'),
'first_name' => $name[0],
'last_name' => $name[1]
]);
Upvotes: 6