【问题标题】:laravel eloquent create without fillable attributelaravel 雄辩的创建没有可填充属性
【发布时间】:2017-07-03 11:08:08
【问题描述】:

这是我的模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;
use Illuminate\Support\Facades\Hash;

class Domain extends BaseModel {

    protected $fillable = ['user_id', 'name', 'username', 'password', 'plan', 'payment', 'status', 'duration'];
    protected $hidden = ['password'];

    protected $rules = [
        'user_id' => [
            'required',
            'integer',
            'exists:users,id'
        ],
        'name' => [
            'required',
            'regex:/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z][a-z0-9-]{0,61}[a-z0-9])$/i',
            'unique:domains,name',
        ],
        'username' => [
            'sometimes',
            'nullable',
            'min:1',
            'max:16',
            'unique:domains,username',
        ],
        'password' => [
            'sometimes',
            'nullable',
            'min:0',
            'max:65',
        ],
        'plan' => [
            'sometimes',
            'nullable',
        ],
        'payment' => [
            'required',
            'string',
            'max:40',
            'in:webpos,cheque'
        ],
        'status' => [
            'sometimes',
            'nullable',
            'integer',
            'in:0,1',
        ],
        'duration' => [
            'sometimes',
            'nullable',
            'integer',
            'min:1',
            'max:10',
        ]
    ];

    protected $attributeNames = [
        'user_id' => 'Domain Sahibi',
        'name' => 'Domain Adı',
        'payment' => 'Ödeme Şekli',
        'status' => 'Domain Durumu',
        'duration' => 'Domain Süresi'
    ];

    public function setUsernameAttribute($value)
    {
        if ( is_null($value) ){
            $value = str_random(16);
        }
        $this->attributes['username'] = strtolower($value);
    }

    public function setPasswordAttribute($value)
    {
        if ( is_null($value) ){
            $value = str_random(16);
        }
        $this->attributes['password'] = Hash::make($value);
    }

    public function setPlanAttribute($value)
    {
        $this->attributes['plan'] = strtolower($value);
    }

    public function setNameAttribute($value){
        $this->attributes['name'] = str_replace('www.', '', $value);
    }

    public function setStatusAttribute($value)
    {
        if ( is_null($value) ){
            $value = 0;
        }

        $this->attributes['status'] = (int)$value;
    }

    public function setDurationAttribute($value)
    {
        if ( is_null($value) ){
            $value = 1;
        }

        $this->attributes['duration'] = $value;
    }
}

这是我的控制器:

$req = $request->only(['user_id', 'name', 'payment', 'status', 'duration']);
$result = $this->domain->add($req);

我收到了这个错误

“无效请求 SQLSTATE[HY000]: 一般错误: 1364 字段'用户名'没有默认值 (SQL: 插入domains (user_id, name, payment, status , duration, updated_at, created_at) 值 (1, deneme-1.com, 支票, 0, 1, 2017-07-03 13:58:03, 2017-07-03 13:58:03 ))"

为什么用户名和密码会转到sql?

我将这些添加到可填充数组中。我想不通为什么不工作?

我不希望用户名和密码随 request 一起出现。我想设置默认 str_random。但不是工作?

问题出在哪里?

为空属性设置默认属性是否正确?

【问题讨论】:

    标签: php mysql eloquent request laravel-5.4


    【解决方案1】:

    因为你把 'username' 放在变量 $fillable var 并且您的数据库中的 必须是 nullable() .

    【讨论】:

      【解决方案2】:

      我通过替换来修复它:

      $req = $request->only(['user_id', 'name', 'payment', 'status', 'duration']);
      

      与:

      $req = array_merge($request->only(['user_id', 'name', 'payment', 'status', 'duration']), ['username' => null, 'password' => null]);
      

      【讨论】:

        猜你喜欢
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 2018-04-13
        • 2019-04-03
        • 1970-01-01
        相关资源
        最近更新 更多