Question:
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'm not able to put the bases inside a select
. It is showing an 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>
Answer:
The data to work with Form::select
needs to be an 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>