GetType () Laravel

4

I'm trying to get the column type of the tables from my BD , and how can I use PHP with Laravel I've tried using GetType to get the types and show on view . But the code is only returning Object . Here is the code:

  

Controller

$input = $request->all();
$tables = $request->input('tables');
$resultados = DB::select('select * from ' .$tables);

$colunas = array();
foreach($resultados as $resultado){
    echo (getType($resultado));
    $colunas[] = ((array)$resultado);
}
dd($colunas);
  

View

<div class="form-group">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
        {!! Form::label('tables', 'Tabelas:', ['class' => 'control-label']) !!}
        <br><br>
            <select class="selectpicker form-control" name="tables" id="tables">
                <option disabled selected> -- Selecione -- </option>
                    @foreach($tables as $table)
                        @foreach ($table as $key => $value)
                            <option value="{{ $value }}">{{ $value }}</option>
                        @endforeach
                     @endforeach
            </select>
    </div>

Is there any other way to get the type?

    
asked by anonymous 30.01.2017 / 14:11

1 answer

4

Use describe no $table = 'user'; // nome da tabela $resultados = DB::select('describe ' .$table); // vai mostrar todos os campos de um determinada tabela // com seus tipos e informações extras. dd($resultados);

Output:

>>> $result = DB::select('describe credits');
=> [
     {#678
       +"Field": "id",
       +"Type": "int(10) unsigned",
       +"Null": "NO",
       +"Key": "PRI",
       +"Default": null,
       +"Extra": "auto_increment",
     },
     {#679
       +"Field": "name",
       +"Type": "varchar(50)",
       +"Null": "NO",
       +"Key": "",
       +"Default": null,
       +"Extra": "",
     },
     {#680
       +"Field": "created_at",
       +"Type": "timestamp",
       +"Null": "YES",
       +"Key": "",
       +"Default": null,
       +"Extra": "",
     },
     {#681
       +"Field": "updated_at",
       +"Type": "timestamp",
       +"Null": "YES",
       +"Key": "",
       +"Default": null,
       +"Extra": "",
     },
     {#664
       +"Field": "active",
       +"Type": "tinyint(1)",
       +"Null": "NO",
       +"Key": "",
       +"Default": "1",
       +"Extra": "",
     },
   ]

Only the Type column:

$result = collect(DB::select('describe credits'))->pluck('Type');

Output

=> Illuminate\Support\Collection {#674
     all: [
       "int(10) unsigned",
       "varchar(50)",
       "timestamp",
       "timestamp",
       "tinyint(1)",
     ],
   }

30.01.2017 / 14:26