2014-09-10 65 views
3

我使用php laravel框架构建了一个web应用程序。当我在数据库上保存模型时,它会进行插入,但不保存模型属性。我看不出如何修复,因为laravel日志没有显示任何错误。任何想法?Laravel with Eloquent不保存数据库的模型属性

有模型:

/** 
* 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 

// 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(); 

?> 

当我执行的PHP代码在屏幕上显示的模型属性,使PHP代码一个插入但不保存属性。将是巨大的,如果有人能帮助我:)


有是解决方案(如果有人有同样的问题或类似)

型号:

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]', 
         'adress'=>'Portland' 
         )); 

     // Show some properties 
     echo '<p>'.$user->name.'</p>'; 
     echo '<p>'.$user->surname.'</p>'; 

     // Saving model on database 
     $user->save(); 

    ?> 
+0

谢谢lowerends和watcher都是对的,现在正在工作。非常感谢你! – proktovief 2014-09-10 19:44:02

回答

0

您不需要在模型本身中为特定模型指定要存储的列名称。 ORM的目的之一就是消除你的责任。你也显然不会工作,因为Eloquent的getter/setter是在父类中指定的,但是你自己定义的属性private不能从任何父/子作用域访问。

我的建议:彻底清除,从您的模型中,这些线路:

private $name; 
private $surname; 
private $mobile; 
private $phone; 
private $mail; 
private $adress; 

在那里删除你的构造以及。如果你想创建一个实例并将其存储在数据库中,你可以这样做:

$user = User::create(array(
    'name' => 'John', 
    'surname' => 'Doe', 
    // ... etc 
)); 

,以后你可以轻松地访问这个实例的属性:

echo $user->name; 
1

你不需要在构造函数中指定这些。您可以使用mass-assignment

型号:

class User extends Eloquent { 

protected $fillable = ['name', 'suname', 'mobile', 'phone', 'mail', 'address']; 

} 

控制器:

$user = new User(['name' => 'John', 'surname' => 'Doe', 'mobile' => '685412578', 'phone' => '9354784125','mail' => '[email protected]', 'address' => 'Portland']); 

$user->save(); 
0

Eloquent类,你的模型扩展,因为你声明为private无法达到的性能。您可以不直接在模型上指定属性,也可以将它们声明为protectedpublic,然后Eloquent将有权访问它们并能将它们保存到数据库中。

0
class User extends Model { 

    protected $table = 'users'; 

    public $timestamps = false; 

} 
+1

关闭时间戳 – user5761094 2016-01-08 05:47:12

+0

请解释你的答案。 – 2016-01-08 19:05:49

相关问题