Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get field count of a table #1604

Closed
ghost opened this issue Jun 11, 2013 · 5 comments
Closed

Get field count of a table #1604

ghost opened this issue Jun 11, 2013 · 5 comments

Comments

@ghost
Copy link

ghost commented Jun 11, 2013

I am using this to get my table names DB::select('SHOW TABLES') and after that I would like to get field count for each tables.

The following query is working when I am not using table prefix in database configuration.
But after adding table prefix, the query duplicates the prefix. (dev_dev_branches instead of dev_branches).

$fields = DB::selectOne("SELECT COUNT(*) AS field_count FROM information_schema.COLUMNS C WHERE table_name = '".$table."' AND TABLE_SCHEMA = '".$database."'");
$num_fields = $fields->field_count;

Is there any laravel 4 function to get field count ?

@franzliedke
Copy link
Contributor

No, there isn't.
Where do you get the value of $table from that it is automatically prepended with the prefix?

(This kind of question is probably better suited for the forums.)

@ghost
Copy link
Author

ghost commented Jun 11, 2013

Getting this from query ( DB::select('SHOW TABLES') ) foreach, solved by using substr_replace().

But I am requesting to add a laravel DB method to get table field count.

@taylorotwell
Copy link
Member

I'm sure you could use the Doctrine schema manager to get this information. Explore that DB::getDoctrineSchemaManager method.

@ghost
Copy link
Author

ghost commented Jun 14, 2013

Thanks. Sorry for the issue report, I am a beginner and noticed the API function now only.

@ghost
Copy link
Author

ghost commented Jun 14, 2013

Updated my code using the API:

$schema = DB::getDoctrineSchemaManager();
$tables = $schema->listTableNames();
foreach($tables as $table){
$fields = $schema->listTableColumns($table);
}

Fixed enum problem here, #1346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants