htmlspecialchars() expects Parameter 1 to be string, Object Given

Asked

Viewed 9,661 times

7

I’m trying to make a button select in the Blade with @foreach to get the list of all Mysql databases. Only the @foreach works, I can get the values, but I’m not getting to put the bases inside a select. Is making a mistake:

htmlspecialchars() expects Parameter 1 to be string, Object Given(View: C: Generator Resources views geradors 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>

1 answer

5


The data to work with Form::select needs to be a array with key and value, example:

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

then format the data so on 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.