How to display a specific word from a string in laravel

You can display the specific word from string by converting the string to an array and after that you can get the value of each array element by passing the key to the array.

Answers 1
  • Display first word from name in laravel view after getting the name of user

    //It will display the first name of user
    @php  $userName =  explode(' ', $user->name);  @endphp
    <p>{{ ucfirst($userName[0]) }}</p>

    This code snippet will help you to get the first word from the user name. Sometimes we have a full name in a single column field and we want to display the first name instead of surname then we can display the first name from the full name in the laravel view without changing the controller or model code in laravel.

  • Back to code snippet queries related laravel