Laravel 5 lists htmlentities() expects parameter 1 to be string

13,843

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) !!}
Share:
13,843

Related videos on Youtube

imperium2335
Author by

imperium2335

PHP, JS, MySQL coder and retired 3D modeler.

Updated on September 15, 2022

Comments

  • imperium2335
    imperium2335 over 1 year

    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?

  • Ridwan Pujakesuma
    Ridwan Pujakesuma almost 8 years
    Thanks @marwelln.. you saved my time. +1
  • Connor Leech
    Connor Leech over 7 years
    Great answer. That processing should be handled in controller and then passed to view. Amen!