Skip to content

Commit

Permalink
Merge pull request #120 from tighten/feat/fillable-trait
Browse files Browse the repository at this point in the history
Merge child class fillable traits with those of the parent class
  • Loading branch information
driftingly authored Jan 12, 2024
2 parents a4d4899 + 0142405 commit f92d949
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function getMorphClass(): string

return (new $parentClass)->getMorphClass();
}

/**
* Get the class name for poly-type collections
*
Expand All @@ -140,4 +140,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 @@ -110,5 +110,14 @@ 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();
});
}
}

0 comments on commit f92d949

Please sign in to comment.