带有 Eloquent 的 Laravel 不会在数据库中保存模型属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25773441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel with Eloquent doesn't save model properties on database
提问by kiewlovsky
I'm buiding an web app using php laravel framework. When I save the model on the database it makes an insert but doesn't save model properties. I can't see how to fix because the laravel log doesn't show any mistake. Any idea?
我正在使用 php laravel 框架构建一个网络应用程序。当我将模型保存在数据库中时,它会插入但不保存模型属性。我不知道如何修复,因为 laravel 日志没有显示任何错误。任何的想法?
There's the model:
有模型:
/**
* 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;
}
And there's php code which create User object and save it to the database
还有创建 User 对象并将其保存到数据库的 php 代码
<?php
// Create an object using model's constructor
$user = new User('John', 'Doe', 685412578, 9354784125, '[email protected]', 'Portland');
// Show some properties
echo '<p>'.$user->getName().'</p>';
echo '<p>'.$user->getSurname().'</p>';
// Saving model on database
$user->save();
?>
When I execute the php code show on screen the model properties and makes an insert but doesn't save the properties. Would be great if someone can help me :)
当我执行 php 代码在屏幕上显示模型属性并进行插入但不保存属性时。如果有人可以帮助我,那就太好了:)
There's is the solution (if someone has the same problem or similar)
有解决方案(如果有人有同样的问题或类似的问题)
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;
And php code:
和 php 代码:
<?php
// Create an object
$user = User::create(array(
'name'=>'John',
'surname'=>'Doe',
'mobile'=>685412578,
'phone'=>9354784125,
'mail'=>'[email protected]',
'adress'=>'Portland'
));
// Show some properties
echo '<p>'.$user->name.'</p>';
echo '<p>'.$user->surname.'</p>';
// Saving model on database
$user->save();
?>
采纳答案by lowerends
As described at http://laravel.com/docs/eloquent#mass-assignment, you need the $fillable
property set for all the properties you want to be mass-assignable. You should therefore uncomment the line starting with protected $fillable = ...
.
如http://laravel.com/docs/eloquent#mass-assignment 中所述,您需要$fillable
为所有想要批量分配的属性设置属性。因此,您应该取消注释以 开头的行protected $fillable = ...
。
回答by Jeff Lambert
You don't need to specify the column names that you're storing for the particular model in the model itself. One of the purposes of the ORM is to remove this responsibility from you. What you have also obviously won't work because Eloquent's getters / setters are specified in a parent class, yet you have the properties themselves defined as private
which is inaccessible from any parent / child scope.
您无需在模型本身中为特定模型指定要存储的列名称。ORM 的目的之一是免除您的这一责任。您所拥有的显然也不起作用,因为 Eloquent 的 getter/setter 是在父类中指定的,但您将属性本身定义为private
无法从任何父/子范围访问的属性。
My suggestion: completely remove these lines from your model:
我的建议:从您的模型中完全删除这些行:
private $name;
private $surname;
private $mobile;
private $phone;
private $mail;
private $adress;
Remove your constructor in there as well. If you want to create an instance and store it in the database, you can do this:
也删除你的构造函数。如果要创建实例并将其存储在数据库中,可以执行以下操作:
$user = User::create(array(
'name' => 'John',
'surname' => 'Doe',
// ... etc
));
And later you can easily access the properties of this instance:
稍后您可以轻松访问此实例的属性:
echo $user->name;
回答by c-griffin
You don't need to specify these in the constructor. You can use mass-assignment
.
您不需要在构造函数中指定这些。您可以使用mass-assignment
.
Model:
模型:
class User extends Eloquent {
protected $fillable = ['name', 'suname', 'mobile', 'phone', 'mail', 'address'];
}
Controller:
控制器:
$user = new User(['name' => 'John', 'surname' => 'Doe', 'mobile' => '685412578', 'phone' => '9354784125','mail' => '[email protected]', 'address' => 'Portland']);
$user->save();
回答by Adrien Delessert
The Eloquent
class that your models extend can't reach the properties because you're declaring them as private
. You can either not directly specify the properties on the model, or you can declare them as either protected
or public
, then Eloquent will have access to them and be able to save them to your database.
Eloquent
您的模型扩展的类无法访问属性,因为您将它们声明为private
. 您可以不直接在模型上指定属性,也可以将它们声明为protected
或public
,然后 Eloquent 将可以访问它们并将它们保存到您的数据库中。
回答by user5761094
class User extends Model {
protected $table = 'users';
public $timestamps = false;
}