Skip to content

Commit

Permalink
Merge branch 'main' into feat/onboard-duster
Browse files Browse the repository at this point in the history
  • Loading branch information
driftingly authored Jan 12, 2024
2 parents 6ab329b + f92d949 commit 0d6761d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,23 @@ protected function getParentClass(): string

return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
}


/**
* Merge the fillable attributes for the model with those of its Parent Class
*
* @return array<string>
*/
public function getFillable()
{
$parentClass = $this->getParentClass();

if ((new ReflectionClass($parentClass))->isAbstract()) {

return $this->fillable;
}
$parentFillable = (new $parentClass)->getFillable();

return array_unique(array_merge($parentFillable, $this->fillable));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Parental\Tests\Features;

use Parental\Tests\Models\Event;
use Parental\Tests\Models\Workshop;
use Parental\Tests\TestCase;

class ChildModelFillablesMergeWithParentModelFillablesTest extends TestCase
{
/** @test */
function child_fillables_are_merged_with_parent_fillables()
{
$workshop = Workshop::create([
'name' => 'Scaling Laravel',
'industry' => 'Technology',
'skill_level' => 'Advanced',
]);

$event = Event::first();

$this->assertEquals($event->name, $workshop->name);
}
}
12 changes: 12 additions & 0 deletions tests/Models/Conference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Parental\Tests\Models;

use Parental\HasParent;

class Conference extends Event
{
use HasParent;

protected $fillable = ['industry'];
}
10 changes: 10 additions & 0 deletions tests/Models/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
protected $fillable = ['name', 'type'];
}
12 changes: 12 additions & 0 deletions tests/Models/Workshop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Parental\Tests\Models;

use Parental\HasParent;

class Workshop extends Event
{
use HasParent;

protected $fillable = ['industry', 'skill_level'];
}
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public function runMigrations()
$table->string('type')->nullable();
$table->timestamps();
});

Schema::create('events', function ($table) {
$table->increments('id');
$table->string('type')->nullable();
$table->string('name');
$table->string('industry')->nullable();
$table->string('skill_level')->nullable();
$table->timestamps();
});
}

protected function getEnvironmentSetUp($app)
Expand Down

0 comments on commit 0d6761d

Please sign in to comment.