2016-05-19 11 views
1

AdvertiseController.phpLaravelのsave()はすべてのフィールドを保存しません

class AdvertiseController extends Controller 
{ 
    ... 
    public function NewAdvertiser() 
    { 
     $advertiser = new Advertiser((string)Uuid::generate(4)); 
     $advertiser->createAdvertiser(); 
     return Redirect::route('dashboard'); 
    } 
    ... 
} 

Advertiser.php

class Advertiser extends Model 
{ 
    ... 
    protected $fillable = ['token']; 

    private $token; 

    function __construct($token) 
    { 
     $this->token = $token; 
    } 

    function createAdvertiser() 
    { 
     //dd($this); 
     $this->save(); 
     //dd($this); 
    } 
    ... 
} 

最初dd($this)次のように出力されます

Advertiser {#181 ▼ 
    #fillable: array:1 [▼ 
    0 => "token" 
    ] 
    -token: "44f1e74b-ad19-4e73-ac2e-b37ffde59e99" 
    #connection: null 
    #table: null 
    #primaryKey: "id" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: [] 
    #original: [] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #guarded: array:1 [▼ 
    0 => "*" 
    ] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    #morphClass: null 
    +exists: false 
    +wasRecentlyCreated: false 
} 

dd($this)

Advertiser {#181 ▼ 
    #fillable: array:1 [▼ 
    0 => "token" 
    ] 
    -token: "295764d5-c45c-4aa9-8ce2-8ed772687fb8" 
    #connection: null 
    #table: null 
    #primaryKey: "id" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: array:3 [▼ 
    "updated_at" => "2016-05-19 11:39:42" 
    "created_at" => "2016-05-19 11:39:42" 
    "id" => 7 
    ] 
    #original: array:3 [▼ 
    "updated_at" => "2016-05-19 11:39:42" 
    "created_at" => "2016-05-19 11:39:42" 
    "id" => 7 
    ] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #guarded: array:1 [▼ 
    0 => "*" 
    ] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    #morphClass: null 
    +exists: true 
    +wasRecentlyCreated: true 
} 

tokenにはマイナス記号がありますが、それは何か関係はありますか?私はそれに関する情報を見つけることができませんでした。フィールドの

データベースの移行:

$table->uuid('token'); 

問題は、すべてのレコードが正常に保存されているということですが、tokenフィールドは空白になっています。

答えて

0

モデルにはいくつか問題があります。 fillableプロパティに値が設定されている必要があります。コンストラクタを削除すると、問題を引き起こしているモデルを正しく入力する能力が失われます。 saveメソッドをラップする必要もありません。また、トークンをクラスのプロパティにすることは、それをモデルの属性にする方法ではありません。 -記号についてのあなたの質問に答えるには、それが私有財産として宣言されたからです。

これは、モデルの外観を示す例です。あなたが本当にコンストラクタをオーバーライドする必要がある場合は

class Advertiser extends Model 
{ 
    protected $fillable = ['token']; 
} 

あなたはそれが属性の配列と親を構築し確認してください。

class Advertiser extends Model 
{ 
    public function __construct(array $attributes = []) 
    { 
     parent::__construct($attributes); 
     // Your code here 
    } 
} 

この場合、コントローラは次のようになります。

class AdvertiseController extends Controller 
{ 
    public function NewAdvertiser() 
    { 
     // Create the model with an attributes array 
     $advertiser = new Advertiser([ 
      'token' => (string)Uuid::generate(4) 
     ]); 

     // Save the model 
     $advertiser->save(); 

     return Redirect::route('dashboard'); 
    } 
} 
関連する問題