Skip to content

Instantly share code, notes, and snippets.

@burlresearch
Created November 24, 2017 03:54
Show Gist options
  • Save burlresearch/92ac9016c18f593a2f564ca063c76622 to your computer and use it in GitHub Desktop.
Save burlresearch/92ac9016c18f593a2f564ca063c76622 to your computer and use it in GitHub Desktop.
Publication Types as JSON Field
<?php
use Faker\Generator as Faker;
$factory->define(App\Publication::class, function (Faker $faker) {
$ptype = \App\PublicationType::all()->random();
$pdata = [];
foreach ($ptype->keys as $key) {
if (rand(0, 2)) {
$pdata[ $key ] = $faker->sentence;
}
}
return [
'title' => $faker->sentence,
'abstract' => $faker->paragraphs(3, 1),
'publisher' => $faker->company,
'type_id' => $ptype->id,
'data' => $pdata,
'date' => $faker->dateTimeBetween('-10 years'),
];
});
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePublicationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('publications', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('type_id');
$table->string('title');
$table->text('abstract');
$table->string('publisher')->nullable();
$table->json('data');
$table->date('date')->nullable();
$table->timestamps();
$table->foreign('type_id')->references('id')->on('publication_types');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('publications');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePublicationTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('publication_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('keys');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('publication_types');
}
}
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(PubTypeSeeder::class);
$this->call(PublicationSeeder::class);
}
}
<?php
use Illuminate\Database\Seeder;
class PublicationSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(\App\Publication::class, 9)->create();
}
}
<?php
use Illuminate\Database\Seeder;
class PubTypeSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
$types = [
'artwork' => [
'artists',
'subject',
'media',
],
'article' => [
'authors',
'main_author',
'year',
'title',
'abstract',
],
'blog' => [
'url',
'title',
'excerpt',
],
];
foreach ($types as $name => $keys) {
\App\PublicationType::create([
'name' => $name,
'keys' => implode(',', $keys),
]);
}
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Publication extends Model
{
protected $guarded = ['id'];
protected $casts = [
'data' => 'json',
];
public function type()
{
return $this->belongsTo(PublicationType::class);
}
public function setDataAttribute($value)
{
$value = array_merge(
array_fill_keys($this->type->keys, null),
$value
);
$this->attributes['data'] = json_encode($value);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PublicationType extends Model
{
protected $guarded = ['id'];
public function publications()
{
return $this->hasMany(Publication::class);
}
public function getKeysAttribute($value)
{
return explode(',', $value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment