【问题标题】:Can I have a Laravel Model without a database table?我可以有一个没有数据库表的 Laravel 模型吗?
【发布时间】:2022-01-04 01:13:48
【问题描述】:

我想要一个具有 Laravel 模型的所有特性和功能的 Laravel 模型,但模型的数据是完全静态的,永远不会改变。我可以为它创建一个数据库表,但这留下了有人直接修改表的可能性。

例如:

    //  Model Data for Log Levels
    name      | icon                | color
    -------------------------------------------
    Emergency | fas fa-ambulance    | red
    Alert     | fas fa-bullhorn     | yellow
    Critical  | fas fa-heartbeast   | orange
    ....

这将是存储在模型文件中的数据,就像它是一个数据库表一样,但永远不能更改。这可能吗?

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    您可以使用寿司

    Eloquent 缺少“数组”驱动程序。

    有时你想使用 Eloquent,但不处理数据库。

    class State extends Model
    {
        use \Sushi\Sushi;
    
        protected $rows = [
            [
                'name' => 'Emergency',
                'icon' => 'fas fa-ambulance',
                'color' => 'red'
            ],
            [
                'name' => 'Alert',
                'icon' => 'fas fa-bullhorn',
                'color' => 'yellow'
            ],
            [
                'name' => 'Critical',
                'icon' => 'fas fa-heartbeast',
                'color' => 'orange'
            ],
        ];
    }
    

    https://github.com/calebporzio/sushi

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    我认为您可以使用简单的模型,而无需创建用于注入数据的表单,以及在第一次(运行迁移时)包含您的数据的播种器。

    【讨论】:

    • 这没有回答问题,因为这仍然会创建一个数据库表。
    猜你喜欢
    • 2013-07-19
    • 2013-09-05
    • 1970-01-01
    • 2018-08-18
    • 2011-01-31
    • 2011-06-13
    • 1970-01-01
    • 2021-09-04
    • 2023-03-15
    相关资源
    最近更新 更多