$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/
The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.
What is Implode Explode in laravel? The implode function implodes an array into a string, and the explode function explodes a string into an array. It's very useful if you've stored your data in strings in files and want to convert those strings into array elements when your Web application runs.12-Aug-2015.
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]
]);
You can use the following code
$full_name = "John Doe";
$name = explode(' ',$full_name);
$first_name = $name[0];
$last_name = $name[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With