传给Blade的Laravel单词在没有解释的情况下变得大写

时间:2016-04-07 10:04:50

标签: php laravel

晚上好,我这里有一个相当奇怪的问题。我无法在网上找到任何有关正在发生的事情的资源。

当我使用控制器中的以下内容在刀片模板中显示信息时:

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

    if ($results != null) {

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

    }

它将传递到我的刀片模板中的每个单词都大写。所以,让我说我从我的数据库中传递了一个完整的段落,我段落中每个单词的每个第一个字母都变为大写。

以下是我看来的剪贴画:

@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

//

另一方面,当我选择使用以下内容将信息传递给我的其他模板时:

$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);

这可以将数据传递到我的Blade模板中,不会以任何方式改变单词的大小写:

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

有没有人也遇到过这个问题?如果是这样,有人可以解释这背后的原因吗?

提前致谢!

1 个答案:

答案 0 :(得分:5)

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));
    }