2017-04-04 85 views
0

我有一個雄辯模型和匹配遷移。我遇到了未設置默認值的問題。我正在使用MySQL。雄辯模型沒有默認

遷移:

Schema::create('cert_jobs', function (Blueprint $table) { 
     $table->bigIncrements('id'); 
     $table->string('key')->unique(); 
     $table->enum('task', [CertJob::TASK_CREATE, CertJob::TASK_REVOKE]); 
     $table->integer('user_id')->unsigned(); 
     $table->string('input_file')->nullable(); 
     $table->string('output_file')->nullable(); 
     $table->enum('format', CertJob::CERT_FORMATS); 
     $table->integer('queue_job_id')->nullable(); 
     $table->integer('status')->default(\App\CertJobStatus::PENDING); 
     $table->text('error')->nullable(); 
     $table->integer('attempts')->default(0); 
     $table->boolean('downloaded')->default(false); 
     // created_at && updated_at 
     $table->timestamps(); 
     $table->softDeletes(); 

     $table->foreign('user_id') 
      ->references('id')->on('users') 
      ->onDelete('cascade'); 
    }); 

型號:

class CertJob extends Model 
{ 
    //... 

    protected $fillable = [ 
     'task', 'input_file', 'format', 'status', 'attempts', 'key', 
    ]; 

    protected $visible = [ 
     'uuid', 'task', 'owner', 'format', 'error', 'job_status', 
     'created_at', 'updated_at', 
    ]; 

    protected $appends = [ 
     'job_status', 'uuid', 'owner' 
    ]; 

    public function getRouteKeyName() 
    { 
     // Auto inject by key instead of ID 
     return 'key'; 
    } 

    public function user(){} 

    private function makePath(String $file){} 

    public function getInputFile(){} 

    public function getOutputFile(){} 

    public function getJobStatusAttribute(){} 

    public function getUuidAttribute(){} 

    public function getOwnerAttribute(){} 

    public function pushToQueue(){} 


    public static function make(string $format, User $owner, UploadedFile $certFile) : CertJob 
    { 
     // Validate 
     $format = strtoupper($format); 
     if (!in_array($format, CertJob::CERT_FORMATS)) { 
      $format = CertJob::CERT_FORMATS[0]; 
     } 

     // Create our cert job 
     $certJob = new CertJob([ 
      'key' => bin2hex(random_bytes(16)), 
      'task' => 'CREATE', 
      'format' => $format, 
     ]); 

     $certJob->user()->associate($owner); 
     $certJob->save(); 

     Log::info($certJob->task . ' Cert Job ' . $certJob->key . ' Created for User:' . $owner->email); 

     // Save input file with certJob ID in filename (easier to identify) 
     $inputFile = $certFile->storeAs('create', 'job_' . str_pad($certJob->id, 4, '0', STR_PAD_LEFT) . '.' . $format, CertJob::FS_DISK); 
     $certJob->input_file = $inputFile; 
     // Delete tmp 
     Storage::delete($certFile); 

     $certJob->save(); 
     return $certJob; 
    } 
} 

當我創建一個CertJob ::使功能CertJob,得到的對象沒有downloadedstatus,或attempts性能。

我試過打電話fresh()零變化。爲什麼遷移的默認值不會傳播到模型?我做錯了嗎?

對於它的價值,我在測試我的模型時發現了這個問題。我在測試課中唯一的魔法是use DatabaseMigrations;

感謝您的幫助!

回答

0

哇我明白了。我不得不返回fresh()的結果。

因此,在make函數的末尾,我將return $certJob更改爲return $certJob->refresh(),它現在可以工作。