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

Related searches

Current Situation : We are creating new database for new registration.

Problem : If any changes done in database migration we need to handle it for previously created database. Or run that migration for each previously created database.

If we run migration for each database no problem.

Question : how to check database table has column for which we are applying condition in query.

Currently I need to fire two queries first for first row and check the existence of that column and then apply condition in where clause. like below

$firstRow = Model::first();
if(isset($firstRow->is_splited)){
    $records = Model::where('is_splited',0)->get(); // this will give error if I don't check column existence for previously created database. 
}else{
    $records = Model::all();
}

Is there any way to achieve in one query. Or any better way to do it ?


Thanks for your time and suggestion.

@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();

Laravel Validation - Check if a column exists, I'm using laravel's awesome validation classes, but I need to know if a column exists in the table (not a row in a given column, the column itself)  Laravel Validation - Check if a column exists Posted 4 years ago by raphadko I'm using laravel's awesome validation classes, but I need to know if a column exists in the table (not a row in a given column, the column itself)

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

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

There's also getColumnListing and hasColumn.

Test attributes/columns existence, I would like to know if the model table has a column named foo. Then I can perform a search in the array to check if the mandatory attributes exists. Model::​hasAttribute($key) https://github.com/laravel/framework/pull/22250. So, if table flights does not exist, then we create it. Of course, it can be used in a positive way – for example, if we want to add a new column to existing table, then we may check if table does exist. Let’s combine this with another example and with another method Schema::hasColumn(), which has two parameters – table name and column name.

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.

Laravel, we can check column is exists or not in tabales using hasColumn. If you also need to remove if column is exists then you can use bellow example. Laravel drop column if exists – We can use migration to drop column if exists using the artisan command. Here in this tutorial we are going to explain how you can drop column in Laravel. Laravel drop column if exists Using Migration | with full Example. Let us understand how to drop drop column if exists Using Migration.

Migration - down: check if table has column, Migration – down: check if table has column Now, if we're doing migrate:​rollback and for whatever reason that column doesn't exist in the table, Eloquent: Recursive hasMany Relationship with Unlimited Subcategories  To get started, let's create an Eloquent model. Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class. The easiest way to create a model instance is using the make:model Artisan command:

Quick Tip for Migrations: Check if Table/Column Already Exists , Quick Tip for Migrations: Check if Table/Column Already Exists of writing a create migration for the same table, or even column that already exist. Eloquent​: Recursive hasMany Relationship with Unlimited Subcategories  Home » Php » Insert a new record if not exist and update if exist, laravel eloquent Insert a new record if not exist and update if exist, laravel eloquent Posted by: admin October 29, 2017 Leave a comment

Laravel validation rule to check if a model exists. You want to use this rule if the standard laravel Rule::exists('table', 'column') is not powerful enough. When you want to add joins to your exist rule, or the advanced Eloquent\Builder features like whereHas this might be for you.

Comments
  • Raut ji good you explained proper way.
  • @pankajkumar: Dhanbad, I mean Dhanyawad :)
  • ha ha ha, Dhanbad was not wrong.. I am from Dhanbad-Jharkhand. Nikesh Ji, I want to join you. please send your Mail or Wtsup for technology share. I am working on the Multiple Vendor Auth concept in Laravel. so I think you can help me. The model is already developed and being used in the project. but in want to improve this. so i am telling. send your mail in p1608088@gmail.com . @Niklesh where you are from?
  • Wow you gave an idea.
  • I know it's not complete but really helped me a lot. Now I can use in_array('col_name',$columns)
  • This one worked for me without having to import anything.