How to retrieve the full name database and show only the first name?

1

I have a table that holds users and their data, the name and saved in a single field. What I intend and get a name of the database for example César Sousa and show in the hour only the Caesar I am using Laravel 5.4 and I rename the user through {{Session::get('nome')}} which is a session that I keep when I log in. I wanted the name of the user in the blade template to show me the first name before the first space.

    
asked by anonymous 28.05.2017 / 19:38

1 answer

1

What you want is to get the first name before the first blank space. In PHP there are a few ways to do this. The simplest and most common solution to this problem is using EXPLODE ( link ).

This method transforms a string into an array, separating each part of the array from a substring.

Example:

explode(' ', 'Carlos Souza de Azevedo'); // ['Carlos', 'Souza', 'de', 'Azevedo']

In this scenario, you want to get the first name. That is, you want to get the first item in the array.

$nomes = explode(' ', 'Carlos Souza de Azevedo');
$primeiroNome = $nomes[0]; // 'Carlos'
    
28.05.2017 / 20:57