【问题标题】:Laravel - Model is saved with empty attribute value (NULL) to databaseLaravel - 模型以空属性值(NULL)保存到数据库
【发布时间】:2020-04-14 19:35:27
【问题描述】:

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

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

<?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();
    ...

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

更新:

$unit 之后的对象save()

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 => "*"
  ]
}

【问题讨论】:

  • $unit->save()的返回值是多少,/storage/logs/laravel.log中记录了什么?
  • 为什么将 $name 和 $description 定义为类的数据成员?问题就在那里。
  • @veelen,返回值为true。而且日志是空的,几分钟前我清空了它,因为它包含大约 22000 行。目前没有任何记录。
  • 你试过评论$name$description来自Model吗?
  • 你迁移中namedescription的字段类型是什么?

标签: php laravel laravel-5


【解决方案1】:

删除这个:

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

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

【讨论】:

  • 你能解释一下为什么会导致这个错误吗?我很好奇^^
  • 是的,这意味着您直接从模型中设置这些属性的值。因此,如果您保存,它将获取这些值(NULL)而不是您发送的内容。它也影响数据检索。如果您已将数据保存在数据库中,然后将这些行添加回来,则当您要检索时,这些值将为空。
  • 花了我几个小时才找到这个。谢谢!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    尝试取消注释$guarded 属性并保护id

    protected $primaryKey = 'id';
    

    我确实认为它与以下属性有关:

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

    这可能意味着一切都受到保护。

    更新

    受保护的属性应该是什么样子。

    protected $guarded = ['id'];
    

    【讨论】:

    • 我的模型中没有 $guarded 属性。不过我加了protected $primaryKey = 'id';又试了一遍,还是不行。
    • 那么你应该添加它......它在你的模型中可用,因为它扩展了使用 Concerns\GuardsAttributes Trait 的 Illuminate\Database\Eloquent\Model。
    • 你使用工匠命令artisan make:model创建新模型吗?
    • 他已经拥有 $fillable 来将他想要保存的属性列入白名单。
    • 不,我没有使用artisan make:model。我认为如果我们使用中间件,我们只需要guarded
    猜你喜欢
    • 2017-09-01
    • 1970-01-01
    • 2014-11-04
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多