如何解决模型如何知道它与什么迁移相关联?
对于菜鸟的问题,我深表歉意。我来自前端,并试图通过学习后端来提高我的开发技能。我对 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
实例变量,将使用类名计算要交互的数据库表名。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。