问题描述
对于菜鸟的问题,我深表歉意。我来自前端,并试图通过学习后端来提高我的开发技能。我对 PHP 相当陌生。我很难理解模型如何知道它与哪个迁移相关联,反之亦然?比如Model类怎么知道往哪个表写数据?
解决方法
您可能需要检查 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
实例变量,将使用类名计算要交互的数据库表名。