2017-04-04 40 views
0

Eloquentモデルと一致する移行があります。私はデフォルト値が設定されていない問題があります。私はMySQLを使用しています。Eloquentモデルにデフォルトがありません

移行:

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 $certJobreturn $certJob->refresh()に変更しました。

関連する問題