Gettype() Laravel

Asked

Viewed 343 times

4

I’m trying to get the type of the columns of the tables of my BD, and how to use PHP with the Laravel I tried to use the GetType to pick up the guys and show on view. But the code is only returning Object. Follows 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 guy?

  • you need the types that are in your Bank table? what comes in $tables (is the name of a table)?

  • What comes in $tables are the columns of the table I select in my database. What I need now is to get the type of each column. Ex: int, varchar.

  • 1

    A DESC tabela wouldn’t solve? will list the name, type and options (Primary key, null allows, default value etc) of column.

  • Lara O que vem em $tables sãos as colunas da tabela me cite examples that comes within this variable?

  • Virgilio, my tabela flame Tabelas, inside of her I have: Id, tabela, campo, tipo and pesquisa. And I want to take the type of each field. Got it? And Rray, I don’t know if DESC table would solve because I didn’t try. I’ll try.

  • Now I understand Lara, because, your code with a s in the end caused me confusion, well the answer I believe answers what you need, please take a look

Show 1 more comment

1 answer

4


Utilize describe in the stating the table name, example:

$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); 

Exit:

>>> $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 column Type:

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

Exit

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

References:

  • @Laragallassi ready ...

Browser other questions tagged

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