staudenmeir /
laravel-merged-relations
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Staudenmeir\LaravelMergedRelations\Eloquent; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
||||
| 6 | use Illuminate\Database\Eloquent\Model; |
||||
| 7 | use Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation; |
||||
| 8 | |||||
| 9 | trait HasMergedRelationships |
||||
| 10 | { |
||||
| 11 | /** |
||||
| 12 | * Define a merged relationship. |
||||
| 13 | * |
||||
| 14 | * @param string $view |
||||
| 15 | * @param string|null $localKey |
||||
| 16 | * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation |
||||
| 17 | */ |
||||
| 18 | public function mergedRelation($view, $localKey = null) |
||||
| 19 | { |
||||
| 20 | return $this->mergedRelationWithModel(static::class, $view, $localKey); |
||||
| 21 | } |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Define a merged relationship with model. |
||||
| 25 | * |
||||
| 26 | * @param string $related |
||||
| 27 | * @param string $view |
||||
| 28 | * @param string|null $localKey |
||||
| 29 | * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation |
||||
| 30 | */ |
||||
| 31 | public function mergedRelationWithModel($related, $view, $localKey = null) |
||||
| 32 | { |
||||
| 33 | $instance = $this->newRelatedInstance($related)->setTable($view); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 34 | |||||
| 35 | $query = (new Builder($instance->getConnection()->query()))->setModel($instance); |
||||
| 36 | |||||
| 37 | $localKey = $localKey ?: $this->getKeyName(); |
||||
|
0 ignored issues
–
show
It seems like
getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 38 | |||||
| 39 | return $this->newMergedRelation($query, $this, $view.'.laravel_foreign_key', $localKey); |
||||
|
0 ignored issues
–
show
$this of type Staudenmeir\LaravelMerge...\HasMergedRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelMerge...ps::newMergedRelation().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Instantiate a new MergedRelation relationship. |
||||
| 44 | * |
||||
| 45 | * @param \Illuminate\Database\Eloquent\Builder $query |
||||
| 46 | * @param \Illuminate\Database\Eloquent\Model $parent |
||||
| 47 | * @param string $foreignKey |
||||
| 48 | * @param string $localKey |
||||
| 49 | * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation |
||||
| 50 | */ |
||||
| 51 | protected function newMergedRelation(EloquentBuilder $query, Model $parent, $foreignKey, $localKey) |
||||
| 52 | { |
||||
| 53 | return new MergedRelation($query, $parent, $foreignKey, $localKey); |
||||
| 54 | } |
||||
| 55 | } |
||||
| 56 |