Laravel雄辩创建空字段 [英] Laravel Eloquent create null field

查看:105
本文介绍了Laravel雄辩创建空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Product extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    public $timestamps = true;
    protected $fillable = ['name'];
    protected $connection = 'pos';

    public function __construct()
    {
        config(['database.connections.pos.database' => 'pos_1']);
        parent::__construct();
    }
}

Product::create(['name' => 'Snack']);



< 30:24',updated_at ='2016-06-26 18:30:24',deleted_at = null

on my db id=1, name=null, created_at='2016-06-26 18:30:24', updated_at='2016-06-26 18:30:24', deleted_at=null

为什么name = null?

Why name=null?

更新帖子

当我使用

$p = new Product();
$p->name = 'Snack';
$p->save();

但为什么 Product :: create(['name'=>小费']); 填充为空?

我在该构造函数中缺少pass数组作为参数。

ah I missing pass array as parameter on that constructor.

        public function __construct(array $attributes)
        {
            config(['database.connections.pos.database' => 'pos_1']);
            parent::__construct($attributes);
        }


推荐答案

可能原因是你再次不要运行父构造函数。

Probably the reason is that you again don't run parent constructor.

而不是:

public function __construct()
{
    config(['database.connections.pos.database' => 'pos_1']);
}

您应该使用:

public function __construct()
{
    config(['database.connections.pos.database' => 'pos_1']);
    parent::__construct();
}

请注意,对于您的雄辩模型,您还有连接属性,所以你可以设置数据库连接的名称。

Be aware that for your Eloquent Model you have also connection property, so you could probably set also name of database connection.

这篇关于Laravel雄辩创建空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆