模型如何知道它与什么迁移相关联?
How does a Model know what migration it is associated with?
对于菜鸟问题,我深表歉意。我来自前端,并试图通过学习后端来提高我的开发技能。我是 PHP 的新手。我很难理解模型如何知道它与哪个迁移相关联,反之亦然?例如,模型 class 如何知道要向什么 table 写入数据?
您可能需要查看 Illuminate\Database\Eloquent\Model
class:
// 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
实例变量,将使用 class 名称计算与之交互的数据库 table 名称。
对于菜鸟问题,我深表歉意。我来自前端,并试图通过学习后端来提高我的开发技能。我是 PHP 的新手。我很难理解模型如何知道它与哪个迁移相关联,反之亦然?例如,模型 class 如何知道要向什么 table 写入数据?
您可能需要查看 Illuminate\Database\Eloquent\Model
class:
// 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
实例变量,将使用 class 名称计算与之交互的数据库 table 名称。