|
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; |
|
|
|
|
|
|
67
|
|
|
$response['status'] = true; |
|
68
|
|
|
|
|
69
|
|
|
return response()->json($response); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.