Check if column exist in Laravel model's table and then apply condition

@DigitalDrifter gave an idea to use Schema class so I just tried like this

I Include Schema class

use Illuminate\Support\Facades\Schema;

And check column by using Schema class, and it also works

$isColExist = Schema::connection("connection_name")->hasColumn('table_name','is_splited');
$q = Acquire::with("block");
if($isColExist){
    $q->where('is_splited',0);
}
$records = $q->get();

Updated solution working in 5.8

if ($object->getConnection()
           ->getSchemaBuilder()
           ->hasColumn($object->getTable(), 'column_name')) {
    // required update here
}

This solution only requires an $object and a 'column_name' to work. The connection and table names are derived from the object itself.


You can do the following to get an array of Column classes:

$columns = $model->getConnection()->getDoctrineSchemaManager()->getColumns();

There's also getColumnListing and hasColumn.