在Laravel中,基于雄辩的模型是否有可能对底层表进行反思,并检索有关列的信息(类型、大小、无符号、默认值等)?
或者这是故意遗漏的?
我觉得能够确定列类型(可以智能地分配一些验证,或者帮助推断表单输入类型和/或填充默认值)是很有用的。
发布于 2014-07-25 23:02:06
我不认为你能做到这一点,“开箱即用”,虽然这将是相对容易实现。像这样的东西,也许?
<?php
class Model extends Eloquent {
public function describe()
{
$table = $this->getTable();
$pdo = \DB::connection()->getPdo();
return $pdo->query("describe $table")->fetchAll();
}
}
$model = new Model;
$columns = $model->describe();
// $columns:
array (
0 =>
array (
'Field' => 'id',
0 => 'id',
'Type' => 'int(10) unsigned',
1 => 'int(10) unsigned',
'Null' => 'NO',
2 => 'NO',
'Key' => 'PRI',
3 => 'PRI',
'Default' => NULL,
4 => NULL,
'Extra' => 'auto_increment',
5 => 'auto_increment',
),
1 =>
array (
'Field' => 'created_at',
0 => 'created_at',
'Type' => 'timestamp',
1 => 'timestamp',
'Null' => 'NO',
2 => 'NO',
'Key' => '',
3 => '',
'Default' => '0000-00-00 00:00:00',
4 => '0000-00-00 00:00:00',
'Extra' => '',
5 => '',
),
...https://stackoverflow.com/questions/24700739
复制相似问题