有没有办法重命名 Lumen 中的模型列?
Is there a way to rename Model columns in Lumen?
我有一个西班牙语数据库,例如:
create table usuarios
(
idUsuario int(6) unsigned zerofill not null auto_increment
primary key,
estado enum('A', 'B') default 'A' not null,
nombre varchar(60) not null,
correo varchar(60) not null,
clave varchar(32) not null,
fecha_alta datetime not null,
fecha_baja datetime null
)
comment 'Usuarios que consumen la API'
;
create index estado
on usuarios (estado)
;
我在 Lumen 中有以下模型:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable;
protected $connection = 'api';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'usuarios';
public $timestamps = false;
protected $fillable = [
'nombre', 'correo',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
// protected $hidden = [
// 'clave',
// ];
}
所以问题是什么?这行得通......但我需要所有 API 英文,其中包括参数......但在这种情况下,Lumen 仅了解创建和更新方法何时接收西班牙语参数。有没有办法像我对 $table = 'usuarios'
那样映射 $filleable = ['nombre', 'correo']
?我希望 $filleable = ['nombre' => 'name', 'correo' => 'email']
之类的东西可以解决问题,但我没有在文档中看到任何内容。
提前致谢!
有可能。使用 Accessors & Mutators.
例如,要从数据库中获取 name 属性,定义一个访问器:
public function getNameAttribute() {
return $this->nombre;
}
要将 name 属性更新到数据库,定义一个修改器:
public function setNameAttribute($value) {
$this->attributes['nombre'] = $value;
}
定义访问器和修改器后,访问和更新 name 属性如下:
$name = $user->name; // access the field nombre
$user->name = 'foo';
$user->save(); // update nombre field
我有一个西班牙语数据库,例如:
create table usuarios
(
idUsuario int(6) unsigned zerofill not null auto_increment
primary key,
estado enum('A', 'B') default 'A' not null,
nombre varchar(60) not null,
correo varchar(60) not null,
clave varchar(32) not null,
fecha_alta datetime not null,
fecha_baja datetime null
)
comment 'Usuarios que consumen la API'
;
create index estado
on usuarios (estado)
;
我在 Lumen 中有以下模型:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable;
protected $connection = 'api';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'usuarios';
public $timestamps = false;
protected $fillable = [
'nombre', 'correo',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
// protected $hidden = [
// 'clave',
// ];
}
所以问题是什么?这行得通......但我需要所有 API 英文,其中包括参数......但在这种情况下,Lumen 仅了解创建和更新方法何时接收西班牙语参数。有没有办法像我对 $table = 'usuarios'
那样映射 $filleable = ['nombre', 'correo']
?我希望 $filleable = ['nombre' => 'name', 'correo' => 'email']
之类的东西可以解决问题,但我没有在文档中看到任何内容。
提前致谢!
有可能。使用 Accessors & Mutators.
例如,要从数据库中获取 name 属性,定义一个访问器:
public function getNameAttribute() {
return $this->nombre;
}
要将 name 属性更新到数据库,定义一个修改器:
public function setNameAttribute($value) {
$this->attributes['nombre'] = $value;
}
定义访问器和修改器后,访问和更新 name 属性如下:
$name = $user->name; // access the field nombre
$user->name = 'foo';
$user->save(); // update nombre field