htmlspecialchars () expects parameter 1 to be string, object given

5

I'm trying to make a select button on the blade with @foreach to get the list of all MySql databases. Only @foreach works, I can get the values, but I can not put the bases inside select . It is giving error:

  

htmlspecialchars () expects parameter 1 to be string, object given (View: C: \ Generator \ resources \ views \ generators \ create.blade.php)

My controller:

public function create(Request $request)
{
    $tables = DB::select('SHOW TABLES');
    //dd($tables);
    //GeradorController->criacontroller($request);
    return view("geradors.create",['tables'=>$tables]);
}

My View:

<div class="col-md-2">
        <div class="panel panel-default">
        <div class="panel-heading">Tabelas:</div>
        @foreach($tables as $table)
        {{ $table }}
        @endforeach
        <select name="tables">
        {!! Form::select('table', $table, ['class' => 'form-control']) !!}
        </select>
        </div>
</div>
    
asked by anonymous 26.01.2017 / 14:07

1 answer

3

The data to work with Form::select must be array with key and value example :

$array = [key1 => valor1, key2 => valor2];

then format the data like this in your controller :

public function create(Request $request)
{
    //Lista todas as Base de Dados de um Banco MySQL
    $tables = DB::select('SHOW DATABASES');

    //Para utilizar no Select utilize o função "collect" e o método "pluck"
    $selectDb = collect($tables)
                        ->pluck('Database','Database')
                        ->toArray();

    return view("geradors.create",[
        'tables'=>$tables, 
        'selectDb' => $selectDb
        ]
    )
}

in view :

<div class="col-md-2">
        <div class="panel panel-default">
        <div class="panel-heading">Tabelas:</div>
        @foreach($tables as $table)
        {{ $table->Database }}
        @endforeach

        {!! Form::select('table', $selectDb, ['class' => 'form-control']) !!}

        </div>
</div>
    
26.01.2017 / 14:23