2014-09-10 3 views
3

저는 PHP laravel 프레임 워크를 사용하여 웹 응용 프로그램을 만들고 있습니다. 모델을 데이터베이스에 저장하면 삽입되지만 모델 특성은 저장하지 않습니다. 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 코드 쇼를 실행하면 데이터베이스

<?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 코드가있다 삽입하지만 속성을 저장하지 않습니다. 누군가가


있다가 솔루션입니다 나를 :) 도울 수 있다면 좋을 것 (사람이 같은 문제 또는 유사한있는 경우)

모델 :

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

답변

2

http://laravel.com/docs/eloquent#mass-assignment에서 설명한대로 $fillable 속성 집합이 필요합니다. 대량으로 할당 할 수있는 모든 속성에 대해 따라서 protected $fillable = ...으로 시작하는 줄의 주석 처리를 제거해야합니다.

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']; 

} 

컨트롤러 : 당신의 모델이 private로 선언하고 있기 때문에 속성에 도달 할 수없는 확장

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

$user->save(); 
0

Eloquent 클래스입니다.모델의 속성을 직접 지정할 수 없거나 protected 또는 public으로 선언 할 수 있습니다. 그런 다음 Eloquent에서 해당 속성에 액세스하여 데이터베이스에 저장할 수 있습니다.

0
class User extends Model { 

    protected $table = 'users'; 

    public $timestamps = false; 

} 
+1

닫기 타임 스탬프 – user5761094

+0

답을 설명해주십시오. –

관련 문제