Passed
Push — master ( 0ba402...934d75 )
by Bas
05:33
created

BelongsTo::addEagerConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 10
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo as IlluminateBelongsTo;
6
7
class BelongsTo extends IlluminateBelongsTo
8
{
9
    /**
10
     * Set the base constraints on the relation query.
11
     *
12
     * @return void
13
     */
14
    public function addConstraints()
15
    {
16
        if (static::$constraints) {
17
            $this->query->where($this->ownerKey, '==', $this->child->{$this->foreignKey});
18
        }
19
    }
20
21
    /**
22
     * Set the constraints for an eager load of the relation.
23
     *
24
     * @param  array  $models
25
     * @return void
26
     */
27
    public function addEagerConstraints(array $models)
28
    {
29
        // We'll grab the primary key name of the related models since it could be set to
30
        // a non-standard name and not "id". We will then construct the constraint for
31
        // our eagerly loading query so it returns the proper models from execution.
32
        $key = $this->ownerKey;
33
34
        $whereIn = $this->whereInMethod($this->related, $this->ownerKey);
0 ignored issues
show
Unused Code introduced by
The assignment to $whereIn is dead and can be removed.
Loading history...
35
36
        $this->query->where($key, 'IN', $this->getEagerModelKeys($models));
37
    }
38
}
39