-
-
Save calebporzio/a63b165b500d491a0c250eb5853e5d94 to your computer and use it in GitHub Desktop.
<?php | |
namespace App\Abilities; | |
use Illuminate\Support\Str; | |
use ReflectionClass; | |
/** | |
* Note: This is a preview of an upcoming package from Tighten. | |
**/ | |
trait HasParentModel | |
{ | |
public function getParentClass() | |
{ | |
static $parentClassName; | |
return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName(); | |
} | |
public function getTable() | |
{ | |
if (! isset($this->table)) { | |
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass())))); | |
} | |
return $this->table; | |
} | |
public function getForeignKey() | |
{ | |
return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey; | |
} | |
public function joiningTable($related) | |
{ | |
$models = [ | |
Str::snake(class_basename($related)), | |
Str::snake(class_basename($this->getParentClass())), | |
]; | |
sort($models); | |
return strtolower(implode('_', $models)); | |
} | |
} |
is this you? thanks.
https://github.com/calebporzio/model-inheritance/blob/master/src/HasParentModel.php
Yes, but here is the official package: it is out now: https://github.com/tightenco/parental
Thanks
I got thislink from https://m.habr.com/ru/post/344728/ and I dont understand getName(){which things must be here?}
Found this snippet to be still be useful in 2024, thanks!
The tightenco/parental package requires adding a type column to the parent, and I'm only utilising the extention of models as shortcuts for global scopes (with concepts that can overlap), so it felt a bit overkill/incompatible for my usecase
Also I believe an improvement can be made, instead of overriding joiningTable()
can just override joiningTableSegment()
(which joiningTable()
now uses internally)
public function joiningTableSegment()
{
return Str::snake(class_basename($this->getParentClass()));
}
Do you have any news about a releasing date? =)