/
BelongsToLang.php
47 lines (39 loc) · 1.45 KB
/
BelongsToLang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace DKulyk\Eloquent;
use DKulyk\Eloquent\Relations\BelongsToLang as BelongsToLangRelation;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* Class BelongsToLang.
*
* @mixin Eloquent
*/
trait BelongsToLang
{
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $lang
* @param string $foreignKey
* @param string $otherKey
* @param string $langKey
* @param string $relation
* @param string $fallback
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsToLang($related, $lang = null, $foreignKey = null, $otherKey = null, $langKey = null, $relation = null, $fallback = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$relation = $caller['function'];
}
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related();
$localKey = $otherKey ?: $this->getKeyName();
return new BelongsToLangRelation($instance->newQuery(), $this, $localKey, $foreignKey, $langKey, $relation, $lang, $fallback);
}
}