插入操作为:
$r = [
'idVisitor' => 4,
'lastname' => 'fg',
'firstname' => '11',
'document_number' => '444',
'idEvent' => 2
];
$g = Visitor::insert($r);
型号为:
class Visitor extends Model
{
public $timestamps = false;
protected $table = 'visitors';
protected $fillable = ['firstname', 'lastname', 'middlename', 'document_number', 'pincode', 'ckecked', 'date_cheked', 'user_cheked', 'company', 'code', 'status', 'idEvent', 'date', 'birthday'];
protected $primaryKey = 'idVisitor';
}
如果要这样做:
$g = Visitor::insert($r);
var_dump($g);
它使我成真,因此Laravel认为它是已落实的。
转储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) DEFAULT NULL,
`ckecked` int(11) DEFAULT '0',
`date_cheked` date DEFAULT NULL,
`user_cheked` int(11) DEFAULT NULL,
`company` varchar(100) DEFAULT NULL,
`code` varchar(100) DEFAULT NULL,
`idEvent` int(11) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1',
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `visitors`
--
ALTER TABLE `visitors`
ADD PRIMARY KEY (`idVisitor`,`idEvent`),
ADD UNIQUE KEY `document_number_UNIQUE` (`document_number`),
ADD UNIQUE KEY `pincode_UNIQUE` (`pincode`),
ADD UNIQUE KEY `code_UNIQUE` (`code`),
ADD KEY `fk_visitors_users1_idx` (`user_cheked`),
ADD KEY `fk_visitors_events1_idx` (`idEvent`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `visitors`
--
ALTER TABLE `visitors`
MODIFY `idVisitor` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `visitors`
--
ALTER TABLE `visitors`
ADD CONSTRAINT `fk_visitors_events1` FOREIGN KEY (`idEvent`) REFERENCES `events` (`idEvent`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_visitors_users1` FOREIGN KEY (`user_cheked`) REFERENCES `users` (`idUser`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
我使用MySQL 5.0.12
我也尝试过:
$g = new Visitor();
$g->lastname = 4;
$g->middlename = 4;
$g->document_number = 444;
$g->firstname = 4;
$g->idEvent = 2;
$g->save();
答案 0 :(得分:1)
请使用
$g = new Visitor();
$g->idVisitor = 4;
$g->firstname= 4;
$g->idVisitor = 4
$g->save();
这是另一种方式
答案 1 :(得分:1)
$ g = new Visitor();呼叫类别没有'()',因此$ g =新访客;