|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Http\Traits; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use MaksimM\CompositePrimaryKeys\Eloquent\Relationships\CompositeBelongsTo; |
|
10
|
|
|
|
|
11
|
|
|
trait CompositeRelationships |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param $related |
|
16
|
|
|
* @param null $foreignKey |
|
|
|
|
|
|
17
|
|
|
* @param null $ownerKey |
|
|
|
|
|
|
18
|
|
|
* @param null $relation |
|
|
|
|
|
|
19
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
20
|
|
|
*/ |
|
21
|
|
|
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) |
|
22
|
|
|
{ |
|
23
|
|
|
// If no relation name was given, we will use this debug backtrace to extract |
|
24
|
|
|
// the calling method's name and use that as the relationship name as most |
|
25
|
|
|
// of the time this will be what we desire to use for the relationships. |
|
26
|
|
|
if (is_null($relation)) { |
|
|
|
|
|
|
27
|
|
|
$relation = $this->guessBelongsToRelation(); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Model $instance |
|
32
|
|
|
*/ |
|
33
|
|
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
// If no foreign key was supplied, we can use a backtrace to guess the proper |
|
36
|
|
|
// foreign key name by using the name of the relationship function, which |
|
37
|
|
|
// when combined with an "_id" should conventionally match the columns. |
|
38
|
|
|
if (is_null($foreignKey)) { |
|
|
|
|
|
|
39
|
|
|
$foreignKey = is_array($instance->getKeyName()) ? array_map(function($key) use ($relation) { |
|
|
|
|
|
|
40
|
|
|
return Str::snake($relation).'_'.$key; |
|
41
|
|
|
}, $instance->getKeyName()) : Str::snake($relation).'_'.$instance->getKeyName(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Once we have the foreign key names, we'll just create a new Eloquent query |
|
45
|
|
|
// for the related models and returns the relationship instance which will |
|
46
|
|
|
// actually be responsible for retrieving and hydrating every relations. |
|
47
|
|
|
$ownerKey = $ownerKey ?: $instance->getKeyName(); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
return $this->newBelongsTo( |
|
50
|
|
|
$instance->newQuery(), $this, $foreignKey, $ownerKey, $relation |
|
|
|
|
|
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Instantiate a new BelongsTo relationship. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
58
|
|
|
* @param \Illuminate\Database\Eloquent\Model $child |
|
59
|
|
|
* @param string $foreignKey |
|
60
|
|
|
* @param string $ownerKey |
|
61
|
|
|
* @param string $relation |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) |
|
65
|
|
|
{ |
|
66
|
|
|
return new CompositeBelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
} |