gpt4 book ai didi

laravel-4 - Eloquent 模型可以检索行中的元数据吗?

转载 作者:行者123 更新时间:2023-12-02 03:34:17 25 4
gpt4 key购买 nike

Laravel 中基于 Eloquent 的模型是否可以自省(introspection)/反射(reflect)底层表并检索有关列的信息(类型、大小、无符号、默认值等)?

或者这是故意遗漏的?

我觉得能够确定列类型会很有用(比如智能地分配一些验证,或者帮助推断表单输入类型和/或填充默认值)。

最佳答案

我不认为你可以在 Laravel 中“开箱即用”地做到这一点,尽管它相对容易实现。也许是沿着这些思路?

<?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 => '',
),
...

关于laravel-4 - Eloquent 模型可以检索行中的元数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24700739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com