SandyBites
SandyBites

Reputation: 173

Laravel words passed to Blade somehow become capitalized without explanation

Good evening guys, I am having a rather strange problem here. I cannot find any resource online either regarding what is happening.

When I display information in my blade template using the following in my controller:

$results = DB::table('datatest') -> get();

    if ($results != null) {

        return view('userview') -> with ('name', $results);

    }

It capitalizes every word passed into my blade template. So let's say I pass an entire paragraph from my database, every first letter from each word in my paragraph becomes capitalized.

Here is a cutout from my view:

@foreach ($name as $name)

<tr>

<td>
{!!Form::label($name -> Author)!!}
</td>

<td>
{!!Form::label($name -> Title)!!}
</td>

<td>
{!!Form::label($name -> Year)!!}
</td>

<td>
{!!Form::label($name -> Abstracts)!!}
</td>

</tr>
@endforeach

//

On the other hand, when I choose to pass information to my other template with the following:

$data = DB::table('datatest')->where('id', $id)->first();

    $Author = $data -> Author;
    $Title = $data -> Title;
    $Year = $data -> Year;
    $Abstracts = $data -> Abstracts;

    $results = array('AUTHOR' => $Author, 'TITLE' => $Title, 'YEAR' => $Year, 'ABSTRACTS' => $Abstracts);

    return view('userview2') -> with ($results);

This is able to pass data into my Blade Template that does not alter the capitalization of the words in any way:

</tr>
<td>{!!Form::label('title', $TITLE)!!}</td>
<td>{!!Form::label('author', $AUTHOR)!!}</td>
<td>{!!Form::label('year', $YEAR)!!}</td>
<td>{!!Form::label('abstracts', $ABSTRACTS)!!}</td>
</tr>

Has anyone also encountered this problem? If so, can anyone explain the reason behind this?

Thanks in advance!

Upvotes: 5

Views: 820

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

That's just how Form::label works. According to the documentation, if you want to get untouched output, you should use labels with two parameters like this:

{!! Form::label('email', 'e-mail address') !!}

Which outputs:

<label for="email">e-mail address</label>

In your first cutout you're passing just one parameter and Form::Label prettyfies this string, so:

{!! Form::label('my email'); !!}

Becomes this:

<label for="my email">My Email</label>

How it works

Label builder checks second parameter and if it doesn't exist or it's null, builder passes label $name to the formatLabel() method which uses ucwords() to capitalize every word's first character.

protected function formatLabel($name, $value)
    {
        return $value ?: ucwords(str_replace('_', ' ', $name));
    }

Upvotes: 5

Related Questions