Completed
Push — master ( 8d65fa...1e9d02 )
by
unknown
9s
created

ProvinceController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Bantenprov\WilayahIndonesia\Http\Controllers;
3
/* Require */
4
use App\Http\Controllers\Controller;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\DB;
7
/* Models */
8
use App\User;
9
use Laravolt\Indonesia\Indonesia;
10
use Laravolt\Indonesia\Models\Province;
11
use Laravolt\Indonesia\Models\City;
12
use Laravolt\Indonesia\Models\District;
13
use Laravolt\Indonesia\Models\Village;
14
15
/* Etc */
16
use Validator;
17
/**
18
 * The ProvinceController class.
19
 *
20
 * @package Bantenprov\WilayahIndonesia
21
 * @author  bantenprov <[email protected]>
22
 */
23
class ProvinceController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
    protected $user;
31
32
    public function __construct()
33
    {
34
		$this->indonesia    = new Indonesia;
35
		$this->province     = new Province;
36
		$this->city         = new City;
37
		$this->district     = new District;
38
		$this->village      = new Village;
39
    }
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function allVillages($id = 36)
46
    {
47
        $villages   = $this->province
48
                        ->where($this->province->getTable().'.id', $id)
49
                        ->select(
50
                            $this->village->getTable().'.id as id',
51
                            DB::raw("CONCAT({$this->city->getTable()}.name,' - ',{$this->district->getTable()}.name,' - ',{$this->village->getTable()}.name) as label")
52
                        )
53
                        ->join(
54
                            $this->city->getTable(),
55
                            $this->province->getTable().'.id','=',$this->city->getTable().'.province_id'
56
                        )
57
                        ->join(
58
                            $this->district->getTable(),
59
                            $this->city->getTable().'.id','=',$this->district->getTable().'.city_id'
60
                        )
61
                        ->join(
62
                            $this->village->getTable(),
63
                            $this->district->getTable().'.id','=',$this->village->getTable().'.district_id'
64
                        )->get();
65
66
        $response['village']    = $villages;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
        $response['status']     = true;
68
69
        return response()->json($response);
70
    }
71
}
72