使用 Eloquant 定义了一个 Model,根据文档和测试写入一行记录。数据行插入但是不符合预期。
这个是我的 Model 文件
<?php
namespace app\model;
use support\Model;
class Staff extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'staff';
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;
    /**
     * 邮箱
     * @var string $email
     */
    private string $email = '';
    /**
     * 密码
     * @var string $password
     */
    private string $password = '';
    /**
     * 姓名
     * @var string $name
     */
    private string $name = '';
    /**
     * 头像
     * @var string $avatar
     */
    private string $avatar = '';
    /**
     * 职位
     * @var string $position
     */
    private string $position = '';
    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }
    /**
     * @param string $email
     * @return Staff
     */
    public function setEmail(string $email): Staff
    {
        $this->email = $email;
        return $this;
    }
    /**
     * @return string
     */
    public function getPassword(): string
    {
        return $this->password;
    }
    /**
     * @param string $password
     * @return Staff
     */
    public function setPassword(string $password): Staff
    {
        $this->password = $password;
        return $this;
    }
    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }
    /**
     * @param string $name
     * @return Staff
     */
    public function setName(string $name): Staff
    {
        $this->name = $name;
        return $this;
    }
    /**
     * @return string
     */
    public function getAvatar(): string
    {
        return $this->avatar;
    }
    /**
     * @param string $avatar
     * @return Staff
     */
    public function setAvatar(string $avatar): Staff
    {
        $this->avatar = $avatar;
        return $this;
    }
    /**
     * @return string
     */
    public function getPosition(): string
    {
        return $this->position;
    }
    /**
     * @param string $position
     * @return Staff
     */
    public function setPosition(string $position): Staff
    {
        $this->position = $position;
        return $this;
    }
    /**
     * 用户归属的部门
     */
    public function departments()
    {
        return $this->belongsToMany(Department::class);
    }
    /**
     * 用户归属的角色
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}
这个是我的测试脚本。
$initAccount = new Staff();
$initAccount->setName("demo");
$initAccount->save();查看数据结果:

上图显示的其他各字段的值异常,起码应当有一个 name 的值是 demo。
staff sql:
CREATE TABLE `space_staff` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `position` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
不是很熟悉Eloquent,但是翻阅Eloquent文档并未发现解决办法。请问我是哪里配置没写对?
你这是一点都不熟悉
https://learnku.com/docs/laravel/8.x/eloquent-mutators/9409#81e641
非常感谢。确实没用过 eloquent.我再仔细翻下文档。
是的呢,之前还带着之前 Doctrine 的思想在写。
不用这么麻烦
1、执行命令
composer require webman/console安装webman命令行2、假设你数据库里有一个user表,里面有
idnicknamepassword三个字段,使用命令./webman make:model user它会根user据表自动生成一个
app/model/User.php文件,并且生成表里idnicknamepassword字段相关的注释,内容大概这样然后你就可以用类似下面代码向数据库里插入数据了
因为有自动生成的注释,phpstom等ide都会自动提示User类里的属性,很方便。
不用手动写将类属性写到类里,也不用写方法。
非常感谢,帮大忙了。因为我是先定义了模型再创建表的,所以console生成的代码 没有那些字段。但由于一直以来习惯 get set 的代码风格,可能还是得改写一下 model 里面的代码。