Laravel 5 lists htmlentities() expects parameter 1

2019-07-01 12:13发布

问题:

I have the following:

\App\Models\Finance\FinanceAccount::lists('name', 'id')

At the top of one of my views but it keeps giving me the error:

htmlentities() expects parameter 1 to be string, array given (View: mysite\views\modals\add.blade.php)

What am I doing wrong?


That makes sense about it being an array, I put it into a select and it's working now:

<div class="form-group">
      {!!Form::label('Account')!!}
      {!!Form::select('account', \App\Models\Finance\FinanceAccount::getSelectOptions(), 1, ['class' => 'form-control'])!!}
  </div>

Is there a way to set the namespace for views, so I don't have to type the full namespace all the time?

回答1:

lists gives an array and that's not something you can echo out with {{ }}. What you need to do is to loop or implode the array if you want to print it's content.

With foreach

@foreach ($list as $item)
    {{ $item }}<br />
@endforeach

With implode

{{ implode(', ', $list) }}

With Form::select

{!! Form::select('foo', $list) !!}

If you don't want to use your namespace (which you never should) in your view, send the data from your controller to your view file.

If you have this in your controller

public function foo() {
    $baz = \App\Models\Finance\FinanceAccount::getSelectOptions();

    return view('bar', compact('baz'));
}

The $baz variable is then available in your view file. So you can do this:

{!! Form::select('foo', $baz) !!}