How to know if a column exists in a table in Laravel?

2

I wonder if there is any way to know if a column exists in Laravel .

For example, I want to sort with data coming from the query string. If the value "column" exists in the table, then I command by that field.

 $coluna = Input::get('coluna');

 if (se_a_coluna_existe_na_tabela('usuarios', $coluna))
 {
     $query->orderBy($coluna);
 }
    
asked by anonymous 29.04.2016 / 19:03

1 answer

3

It's not that hard, you can just use the following code

if(Schema::hasColumn('users', 'email')) ; //verifica se a tabela users tem a coluna email
{
    $query->orderBy($coluna);
}

Soon!

If you have any questions, try to take a look at the laravel documentation.

link

    
29.04.2016 / 22:09