2017-03-01 73 views
2

我尝试将我的模型保存到数据库。模型保存到数据库中,但属性的值未设置并保留为空。Laravel - 模型保存为空属性到数据库

enter image description here

这是一个小模型,只有几个属性:

<?php 
namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Unit extends Model 
{ 
    public $fillable = ['name', 'description', 'created_at', 'updated_at']; 

    protected $table = 'units'; 

    // Allowed fields from forms 
    // protected $guarded = ['ic']; 

    /** 
    * @var string 
    */ 
    public $name = NULL; 

    /** 
    * @var string 
    */ 
    public $description = NULL; 
} 

控制器存储功能:

/** 
* 
* @param Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store(Request $request) 
{ 
    $this->validate(request(), [ 
     'name' => 'required|max:80|regex:/^[\pL\s\-]+$/u', //regex: only allows letters, hyphens and spaces explicitly', 
     'description' => 'required', 
     'image' => 'required|image', 
    ]); 

    $unit = new Unit(); 

    $unit->description = $request->description; 
    $unit->name = $request->name; 

    echo $unit->description . PHP_EOL; //Outputs "example description" 
    echo $unit->name . PHP_EOL; //Outputs: "example name" 

    $unit->save(); 
    ... 

我很困惑,它应该工作。我究竟做错了什么?

UPDATE:

对象$unitsave()

Unit {#190 ▼ 
    +fillable: array:2 [▼ 
    0 => "name" 
    1 => "description" 
    ] 
    #table: "units" 
    +name: "example name" 
    +description: "example description" 
    #connection: null 
    #primaryKey: "id" 
    #keyType: "int" 
    +incrementing: true 
    #with: [] 
    #perPage: 15 
    +exists: true 
    +wasRecentlyCreated: true 
    #attributes: array:3 [▼ 
    "updated_at" => "2017-03-01 10:18:59" 
    "created_at" => "2017-03-01 10:18:59" 
    "id" => 27 
    ] 
    #original: array:3 [▼ 
    "updated_at" => "2017-03-01 10:18:59" 
    "created_at" => "2017-03-01 10:18:59" 
    "id" => 27 
    ] 
    #casts: [] 
    #dates: [] 
    #dateFormat: null 
    #appends: [] 
    #events: [] 
    #observables: [] 
    #relations: [] 
    #touches: [] 
    +timestamps: true 
    #hidden: [] 
    #visible: [] 
    #guarded: array:1 [▼ 
    0 => "*" 
    ] 
} 
+2

$ unit-> save()的返回值是什么,以及/storage/logs/laravel.log中记录的内容是什么? – veelen

+0

为什么您将$ name和$ description定义为您班级的数据成员?问题在那里。 – Torgheh

+0

@veelen,返回值为'true'。日志是空的,几分钟前我清空它,因为它包含了大约22000行。现在没有任何记录。 – Black

回答

4

删除此:

/** 
* @var string 
*/ 
public $name = NULL; 

/** 
* @var string 
*/ 
public $description = NULL; 
+0

你能解释为什么这会导致错误吗?我很好奇^^ –

+0

是的,这意味着你直接从模型中设置这些属性的值。所以如果你保存,它会得到这些值(NULL),而不是你发送的内容。它也影响数据检索。如果您已将数据保存到数据库中,然后再添加这些行,则当您想要检索时,这些值将为空。 –

+0

花了我几个小时才找到这个。谢谢! –

0

尝试取消对$guarded属性和守卫id

protected $primaryKey = 'id'; 

我不认为它会是与以下属性:

#guarded: array:1 [▼ 
    0 => "*" 
] 

这可能意味着一切都在守护着。

更新

如何守护属性应该是这样。

protected $guarded = ['id']; 
+0

我的模型中没有'$ guarded'属性。 但是,我添加了'protected $ primaryKey ='id';'并再次尝试,但它不起作用。 – Black

+1

然后您应该添加它......它在您的模型中可用,因为它扩展了使用Concerns \ GuardsAttributes Trait的Illuminate \ Database \ Eloquent \ Model。 –

+0

您是否使用artisan命令'artisan make:model'创建新模型? –

1

如前所述,您必须从模型类中删除这两个属性。这是因为当这些属性被定义时,将不会有神奇的__set()运行,因此$obj->attributes[]将不会被填充值。