Completed
Push — master ( 82eee4...bc4816 )
by
09:25
created

WithOnlyTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A withOnly() 0 13 3
1
<?php
2
3
namespace PHPHub\Repositories\Eloquent\Traits;
4
5
trait WithOnlyTrait
6
{
7
    /**
8
     * Load relations and specific columns.
9
     *
10
     * @param array|string $relation
11
     * @param null         $columns
12
     * @param bool         $with_trashed
13
     *
14
     * @return $this
15
     */
16
    public function withOnly($relation, $columns = null, $with_trashed = false)
17
    {
18
        $this->model = $this->model->with([$relation => function ($query) use ($columns, $with_trashed) {
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
            if ($with_trashed) {
20
                $query->withTrashed();
21
            }
22
            if ($columns) {
23
                $query->select(array_merge(['id'], $columns));
24
            }
25
        }]);
26
27
        return $this;
28
    }
29
}
30