Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use php explode in laravel?

Tags:

laravel

 $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/

like image 727
TheBAST Avatar asked Feb 03 '17 05:02

TheBAST


People also ask

What does explode () do in PHP?

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?

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.


2 Answers

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]
          ]);
like image 170
Alexey Mezenin Avatar answered Sep 28 '22 00:09

Alexey Mezenin


You can use the following code

$full_name = "John Doe";
$name = explode(' ',$full_name);
$first_name = $name[0];
$last_name = $name[1];
like image 28
Manvir Singh Avatar answered Sep 28 '22 00:09

Manvir Singh