Laravel生成数据库的良好实践

时间:2020-04-05 09:16:44

标签: database laravel eloquent

我需要帮助来正确设计我的数据库。我与Laravel / Eloquent(ORM)合作。

my diagram represent that I want

实际上我的模型是这样的:

特殊类:

class Specie extends Model 
{

protected $fillable = ['name'];

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function animal()
{
    return $this->hasOne('App\Animal', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function missingAnimal()
{
    return $this->hasOne('App\MissingAnimal', 'id');
}
}

动物类:

class Animal extends Model
{
/**
 * Indicates if the IDs are auto-incrementing.
 * 
 * @var bool
 */
public $incrementing = false;

/**
 * @var array
 */
protected $fillable = ['name', 'number_legs', 'others'];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function species()
{
    return $this->belongsTo('App\Species', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function cat()
{
    return $this->hasOne('App\Cat', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function dog()
{
    return $this->hasOne('App\Dog', 'id');
}
}

缺少动物类:

class MissingAnimal extends Model
{
/**
 * Indicates if the IDs are auto-incrementing.
 * 
 * @var bool
 */
public $incrementing = false;

/**
 * @var array
 */
protected $fillable = ['name', 'number_legs', 'others', 'extinction_reason'];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function species()
{
    return $this->belongsTo('App\Species', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function dinosaur()
{
    return $this->hasOne('App\Dinosaur', 'id');
}
}

狗类:

class Dog extends Model
{
/**
 * Indicates if the IDs are auto-incrementing.
 * 
 * @var bool
 */
public $incrementing = false;

/**
 * @var array
 */
protected $fillable = ['name'];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function animal()
{
    return $this->belongsTo('App\Animal', 'id');
}
}

恐龙类:

等级恐龙扩展模型 { / ** *表示ID是否自动递增。 * * @var bool * / public $ incrementing = false;

/**
 * @var array
 */
protected $fillable = ['name'];

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function missingAnimal()
{
    return $this->belongsTo('App\MissingAnimal', 'id');
}
}

这样做是一种好习惯吗?还是在laravel上使用'morph ...方法更好。

0 个答案:

没有答案