Commit b8853b0f authored by Antoine Le Gonidec's avatar Antoine Le Gonidec

Archive/Script relationship — Store required/optional archive status

parent 05c77819
......@@ -15,7 +15,7 @@ class Archive extends Model {
*
* @var array
*/
protected $hidden = [ 'archive_id' ];
protected $hidden = [ 'archive_id', 'pivot' ];
/**
* Get the archive that requires the current archive
......@@ -40,7 +40,8 @@ class Archive extends Model {
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function scripts() {
return $this->BelongsToMany(Script::class);
return $this->BelongsToMany(Script::class)
->withPivot('required');
}
/**
......@@ -51,7 +52,7 @@ class Archive extends Model {
public function getScriptsAttribute() {
$scripts = $this->scripts()->get();
$scripts->each(function ($script) {
$script->makeHidden([ 'archives', 'pivot' ]);
$script->makeHidden([ 'archives' ]);
});
return $scripts;
}
......
......@@ -10,13 +10,21 @@ class Script extends Model {
*/
protected $appends = [ 'games', 'archives' ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [ 'pivot' ];
/**
* Get the archives supported by current script
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function archives() {
return $this->BelongsToMany(Archive::class);
return $this->BelongsToMany(Archive::class)
->withPivot('required');
}
/**
......@@ -27,7 +35,8 @@ class Script extends Model {
public function getArchivesAttribute() {
$archives = $this->archives()->get();
$archives->each(function ($archive) {
$archive->makeHidden([ 'archive', 'scripts', 'pivot' ]);
$archive->makeHidden([ 'archive', 'scripts' ]);
$archive->required = (bool) $archive->pivot->required;
});
return $archives;
}
......
......@@ -17,8 +17,10 @@ class CreateArchivesTable extends Migration {
$table->string('url')->nullable();
$table->string('type');
$table->string('md5')->unique();
$table->string('version');
$table->integer('size')->unsigned();
$table->string('version')
->nullable();
$table->unsignedInteger('size')
->nullable();
});
}
......
......@@ -22,6 +22,8 @@ class AddRelationshipBetweenArchivesAndScripts extends Migration {
->references('id')
->on('archives')
->onDelete('cascade');
$table->boolean('required')
->default(true);
});
}
......
......@@ -2925,7 +2925,12 @@ class ArchivesAndScriptsRelationshipsSeeder extends Seeder {
foreach ( $this->relationships as $relationship ) {
$archive = Archive::where('md5', $relationship['md5'])->first();
$script_id = Script::where('file', $relationship['file'])->pluck('id')->first();
$archive->scripts()->attach($script_id);
if ( isset($relationship['required']) ) {
$required = $relationship['required'];
} else {
$required = true;
}
$archive->scripts()->attach($script_id, [ 'required' => $required ]);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment