【问题标题】:How does a Model know what migration it is associated with?模型如何知道它与什么迁移相关联?
【发布时间】:2021-04-20 22:35:50
【问题描述】:
对于这个菜鸟问题,我深表歉意。我来自前端,并试图通过学习后端来提高我的开发技能。我对 PHP 相当陌生。我很难理解模型如何知道它与哪个迁移相关联,反之亦然?例如,Model 类如何知道将数据写入哪个表?
【问题讨论】:
标签:
php
laravel
model
database-migration
【解决方案1】:
您可能需要检查Illuminate\Database\Eloquent\Model 类:
// Copied from the framework.
namespace Illuminate\Database\Eloquent;
abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table;
/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this)));
}
}
所以如果$table实例变量没有设置,那么与之交互的数据库表名将使用类名来计算。