首页 > 解决方案 > Laravel 命名约定,相同的实体还是单独的实体?

问题描述

所以我正在研究一个房地产系统,我们有属性,然后我们有属性类型(可以是房屋、餐厅、土地等)。所以难题是:我们是将属性类型视为属性的一部分,还是将它们视为单独的实体

控制器:

PropertiesController
PropertyTypesController

或者

PropertiesController
TypesController

模型:

Property
PropertyType

或者

Property
Type

看法:

views/properties/index.blade.php
views/properties/show.blade.php
views/properties/edit.blade.php
views/properties/types/index.blade.php
views/properties/types/show.blade.php
views/properties/types/edit.blade.php
views/properties/types/create.blade.php

或者

views/properties/index.blade.php
views/properties/show.blade.php
views/properties/edit.blade.php
views/properties/create.blade.php

views/property_types/index.blade.php
views/property_types/show.blade.php
views/property_types/edit.blade.php
views/property_types/create.blade.php

标签: laravelnaming-conventionslaravel-7

解决方案


我认为这里最重要的问题是这个。

当前或将来是否有可能存在另一种类型的模型/概念?例如 PaymentTypes 或 UserTypes 等等。

如果您已经看到了发生这种情况的最轻微的可能性,我肯定会选择更详细的选项 PropertyTypes。还要记住,当涉及到关系时,您仍然可以使它们不那么冗长,以使您的代码保持简单,例如这样。

public function types() {
    return $this->belongsToMany('App\PropertyType');
}

推荐阅读