Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel words passed to Blade somehow become capitalized without explanation

Tags:

php

laravel

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!

like image 315
SandyBites Avatar asked Apr 07 '16 10:04

SandyBites


People also ask

What is @stack in Laravel blade?

One of blade directive @stack('scripts') is very helpful when you have javascript need to execute in the child page. I create a fresh Laravel installation to demo an example. I have to make auth scaffolding with laravel/ui package because I'm using Laravel 6.

How do you capitalize words in Laravel?

“capitalize string in php laravel” Code Answer's$bar = ucwords($bar); // HELLO WORLD! $bar = ucwords(strtolower($bar)); // Hello World! $bar = ucwords($foo); // Hello|world!

Why does Laravel use the blade template engine?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .

What are the two primary benefits of Laravel blade?

Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.


1 Answers

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));
    }
like image 172
Alexey Mezenin Avatar answered Sep 30 '22 05:09

Alexey Mezenin