팀 루이스 :
그래서, 나는 다음과 같은 모델을 가지고 :
class Recursive extends Model {
public function __construct() {
parent::__construct();
}
// ...
}
class Place extends Recursive {
protected $table = 'places';
protected $fillable = ['name', 'parent_id'];
// ...
}
다음 코드는 새를 만드는 데 사용됩니다 Place
:
$place = Place::create([
'name' = 'Second',
'parent_id' => 1
]);
데이터베이스에 다음과 같은 기록이 결과 :
| Actual | Expected |
---------------------------------------------------------
| id | name | parent_id | id | name | parent_id |
| 1 | 'Top' | NULL | 1 | 'Top' | NULL |
| 2 | NULL | NULL | 2 | 'Second' | 1 |
당신이 볼 수 있듯이, 설정되는 유일한 값은 자동 증가입니다 id
열입니다. 내가 만들기 위해 노력하고있어 2 열은에 fillable
배열하고 모델이 생성되지만 제대로 연결되지.
사람이 전에이 문제를 건너 했습니까? 나는 같은 다른 방법을 사용할 수 있습니다 알고
$place = new Place();
$place->name = 'Second';
$place->parent_id = 1;
$place->save();
그러나 이것은이 코드를 사용하고 발견, 나는이 같은하지 잃게 기능을 원합니다하지 만입니다.
편집 :하여에 대해 다음 쿼리 로그 프로그램 활성화 create()
전화 :
array (
'query' => 'insert into `places` () values ()',
'bindings' =>
array (
),
'time' => 1.26,
),
또한 편집 : 사용 MySQL의 로그는 상기와 동일한 출력을 가지고있다. 를 되 돌리는의 Miken32의 제안에 따라 extends
에 Model
예상대로 작동합니다 :
array (
'query' => 'insert into `places` (`name`, `parent_id`) values (?, ?)',
'bindings' =>
array (
0 => 'Second',
1 => '1'
),
'time' => 1.21,
),
miken32 :
당좌 Illuminate\Database\Eloquent\Model
클래스를 생성자는 다음과 같습니다 :
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
그러나, 당신은 당신이 오버라이드 된 Recursive
클래스 :
public function __construct()
{
parent::__construct();
}
이 성공적으로 쿼리를 작성 할 수 없습니다 때문에 속성은 생성자에 전달되는되지 않았다. 당신은 아무것도 아니에요 이후 생성자를 제거하거나이를 대신 사용할 수 있습니다 :
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}