Home > Net >  Can I have a Laravel Model without a database table?
Can I have a Laravel Model without a database table?

Time:11-26

I would like to have a Laravel Model with all of the features and functions of a Laravel Model, but the data for the model is completely static and will never change. I could create a database table for it, but that leaves the possibility of someone modifying the table directly.

For example:

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

This would be the data, stored in the model file the same as if it was a DB table, but can never be changed. Is this possible?

CodePudding user response:

You can use Sushi for that.

Eloquent's missing "array" driver.

Sometimes you want to use Eloquent, but without dealing with a database.

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

CodePudding user response:

I think you can use a simple model, without creating forms for injecting data, and a seeder to inculde your data at first time (when you run migrate).

  • Related