为什么没有在Laravel中插入数据?

时间:2019-03-08 07:17:01

标签: mysql laravel-5

我尝试将数组插入表中

插入数据为:

$item = [
  "lastname" => "Husey"
  "firstname" => "Fik"
  "middlename" => "Akif"
  "birthday" => "1981-04-09"
  "company" => "XXX"
  "document_number" => 16428285.0
  "pincode" => "QT0FE12"
  "code" => 19283746564923.0
  "idEvent" => "17"
]

插入模型:

Visitor::create($item);

模型访问者为:

class Visitor extends Model
{
    public $timestamps = false;

    public $table = 'visitors';

    public $fillable = [
    'firstname', 
    'lastname', 
    'middlename', 
    'document_number', 
    'pincode', 
    'ckecked', 
    'date_cheked',
    'user_checked',
    'company',
    'code',
    'idEvent',
    'date',
    'birthday'
    ];
}

转储SQL为:

CREATE TABLE `visitors` (
  `idVisitor` int(11) NOT NULL,
  `firstname` varchar(45) NOT NULL,
  `lastname` varchar(45) NOT NULL,
  `middlename` varchar(45) DEFAULT NULL,
  `document_number` varchar(45) NOT NULL,
  `pincode` varchar(10) NOT NULL,
  `ckecked` int(11) DEFAULT '0',
  `date_cheked` date DEFAULT NULL,
  `user_checked` int(10) UNSIGNED DEFAULT NULL,
  `company` varchar(100) DEFAULT NULL,
  `code` varchar(100) DEFAULT NULL,
  `idEvent` int(10) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我没有任何异常或错误。

如果要进行以下操作:

$s = Visitor::create($item);
dd($s);

它返回完整的模型数据,但数据库表为空!

2 个答案:

答案 0 :(得分:0)

我认为您想念您

Dictionary:
[
 {"Venue":"Hotel", "Date":"20190101", "08":{ "IncomingCount":15 }, "10":{ "IncomingCount":03 } },
 {"Venue":"Beach", "Date":"20190101", "07":{ "OutgoingCount":30 }, "08":{ "IncomingCount":93 }, "10":{ "IncomingCount":45, "OutgoingCount":15 } },
 {"Venue":"Hotel", "Date":"20190103", "07":{ "OutgoingCount":05 }, "10":{ "OutgoingCount":15 } }
]

答案 1 :(得分:0)

您在数组中缺少逗号,并且代码运行正常,请以此更新代码。

 $item = [
        "lastname" => "Husey",
        "firstname" => "Fik",
        "middlename" => "Akif",
        "birthday" => "1981-04-09",
        "company" => "XXX",
        "document_number" => 16428285.0,
        "pincode" => "QT0FE12",
        "code" => 19283746564923.0,
        "idEvent" => "17"
    ];

    $visitor = Visitor::create($item);

    return $visitor;