我正在使用 php laravel 框架构建一个网络应用程序。当我将模型保存到数据库时,它会进行插入,但不会保存模型属性。我不知道如何修复,因为 laravel 日志没有显示任何错误。任何想法?
有模型:
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'test';
// protected $fillable = array('name', 'surname', 'mobile', 'phone', 'mail', 'adress');
// Model properties
private $name;
private $surname;
private $mobile;
private $phone;
private $mail;
private $adress;
// Model constructor
function __construct($name, $surname, $mobile, $phone, $mail, $adress) {
$this->name = $name;
$this->surname = $surname;
$this->mobile = $mobile;
$this->phone = $phone;
$this->mail = $mail;
$this->adress = $adress;
}
还有 php 代码创建 User 对象并将其保存到数据库
<?php
// Create an object using model's constructor
$user = new User('John', 'Doe', 685412578, 9354784125, '[email protected] /cdn-cgi/l/email-protection', 'Portland');
// Show some properties
echo '<p>'.$user->getName().'</p>';
echo '<p>'.$user->getSurname().'</p>';
// Saving model on database
$user->save();
?>
当我执行 php 代码时,会在屏幕上显示模型属性并进行插入,但不保存属性。如果有人能帮助我那就太好了:)
这就是解决方案(如果有人有相同或类似的问题)
Model:
protected $table = 'test';
// Fields on databse to save model properties
protected $fillable = array('name', 'surname', 'mobile', 'phone', 'mail', 'adress');
// Model properties
private $name;
private $surname;
private $mobile;
private $phone;
private $mail;
private $adress;
和PHP代码:
<?php
// Create an object
$user = User::create(array(
'name'=>'John',
'surname'=>'Doe',
'mobile'=>685412578,
'phone'=>9354784125,
'mail'=>'[email protected] /cdn-cgi/l/email-protection',
'adress'=>'Portland'
));
// Show some properties
echo '<p>'.$user->name.'</p>';
echo '<p>'.$user->surname.'</p>';
// Saving model on database
$user->save();
?>