Laravel模型属性

时间:2019-02-12 13:05:02

标签: laravel-5

我不确定如何最好地问这个问题。我正在尝试编写DRY代码,并且有一个Job模型。作业可以具有类型(例如:全职,兼职等)。如果我只想定义一次类型,该将它们放在哪里可以在任何地方使用它们?是执行此操作的最佳方法,例如:

class Job extends Model
{
    private static $jobTypes = [
        'full-time',
        'part-time',
    ];

    public static function jobTypes()
    {
        return self::$jobTypes;
    }
}

还是做一个完全独立的类更有意义/增加灵活性?

class JobTypes
{
    private static $jobTypes = [
        'full-time',
        'part-time',
    ];

    public static function jobTypes()
    {
        return self::$jobTypes;
    }
}

还有其他...

1 个答案:

答案 0 :(得分:0)

根据要求,您的第一反应将是最佳实践:

version: '3.7'

networks:
  network-dev:
    driver: bridge

services:
  webapp:
    image: project
    container_name: container-webapp
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:80
    networks:
      - "network-dev"
    ports:
      - "80"
    depends_on:
      - "db"

  db:
    image: postgres:latest
    container_name: container-db
    environment: 
      - "POSTGRES_USER=username"
      - "POSTGRES_PASSWORD=password"
      - "POSTGRES_DB=projectdb"
    restart: always
    ports:
      - "13650:5432"
    networks:
      - "network-dev"

但是,有时将所有这些变量都定义在一个位置是有益的,以便您可以轻松地对其进行修改。因此,如果您出于各种模型或出于某种原因而拥有更多这样的静态选项,我建议您使用配置文件并在以下位置定义它们:version: '3.7' networks: network-prod: driver: bridge services: webapp: image: alicoskun/project:latest container_name: container-webapp environment: - ASPNETCORE_ENVIRONMENT=Production - ASPNETCORE_URLS=http://+:80 networks: - "network-prod" ports: - "80" depends_on: - "db" db: image: postgres:latest container_name: container-db environment: - "POSTGRES_USER=username" - "POSTGRES_PASSWORD=password" - "POSTGRES_DB=projectdb" restart: always ports: - "13650:5432" networks: - "network-prod"

然后您可以从任何地方访问它们,就像这样:

class Job extends Model { private static $jobTypes = [ 'full-time', 'part-time', ]; public static function jobTypes() { return self::$jobTypes; } }

有道理吗?