我是 Laravel 的新手。请指导我。
端口是模型,坐标是无数据库模型。端口模型中有 2 个浮点数 ( lat and long
)。当从数据库加载一个 Port 时,2 个浮点数被转换为一个 Coordinates 对象。
我的第一个问题是,如何制作具有两个属性的非数据库模型?
我的第二个问题是,如何使用 Coordinates 对象在 Port Model 中制作 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"];
}
}
最佳答案
据我所知,每个 Eloquent 模型都应该链接到数据库中的一个表。只需添加 latitude
和 longitude
作为您 migration 中的列创建 port
table 。
关于laravel - 如何在 Laravel 中制作非数据库模型的两个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53170404/