Skip to content

Commit

Permalink
feat: fillable arrays are merged
Browse files Browse the repository at this point in the history
  • Loading branch information
gcavanunez committed Nov 8, 2023
1 parent b023d60 commit 65e3a52
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,12 @@ protected function getParentClass(): string
*/
public function getFillable()
{
// @todo currently failing when parent class is abstract
// $parentFillable = (new \ReflectionClass((new \ReflectionClass($this))->getParentClass()));
// if ($parentFillable->isAbstract()) {
// return ['*'];
// }

try {
$parentClass = $this->getParentClass();
$parentFillable = (new $parentClass)->getFillable();
$arr = array_unique(array_merge($parentFillable, $this->fillable));
return $arr;
} catch (\Throwable $th) {

$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,22 @@
<?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);
}
}
11 changes: 11 additions & 0 deletions tests/Models/Conference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Parental\Tests\Models;

use Parental\HasParent;

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

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Model;

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

Please sign in to comment.