如何在Laravel中使无数据库模型成为两个属性

时间:2018-11-06 10:51:24

标签: laravel laravel-5 laravel-4 laravel-5.1

我是Laravel的新手。请引导我。

端口是一个模型,坐标是一个无数据库模型。端口模型中有2个浮点(lat and long)。从数据库中加载端口后,这两个浮点型将转换为Coordinates对象。

我的第一个问题是,如何创建具有两个属性的非数据库模型?

我的第二个问题是,如何在带有“坐标”对象的“端口模型”中强制转换2个浮点数?

这是我的具有两个属性的代码坐标模型无数据库模型

class Coordinates extends Model
{

//Add attribute
protected $attributes = ['latitude', 'longitude'];
}

这是带有

的端口模型
class Port extends Contracts\AppModel
 {
    protected $coordinates = Coordinates::class;

    protected $fillable=[
        'un_latitude',
        'un_longitude',
    ];

    function __construct(array $attributes = array())
    {
         $this->coordinates = Coordinates::class;
         $this->coordinates->latitude = $attributes["un_latitude"];
         $this->coordinates->longitude = $attributes["un_longitude"];
    }

}

1 个答案:

答案 0 :(得分:0)

据我了解,每个雄辩模型都应该链接到数据库中的一个表。只需将latitudelongitude添加为migration中的列即可创建port表。