1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
class Device extends Model |
10
|
|
|
{ |
11
|
|
|
use SoftDeletes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The attributes that are mass assignable. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $fillable = [ |
19
|
|
|
'name', 'location_id' |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public $timestamps = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the location for the device |
26
|
|
|
*/ |
27
|
|
|
public function location() |
28
|
|
|
{ |
29
|
|
|
return $this->belongsTo('App\Location', 'location_id'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the site for the device |
34
|
|
|
*/ |
35
|
|
|
public function site() |
36
|
|
|
{ |
37
|
|
|
return $this->belongsTo('App\Site'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Update the device with the matching id with a new location id |
42
|
|
|
* |
43
|
|
|
* @param int $id |
44
|
|
|
* @param int $location_id |
45
|
|
|
*/ |
46
|
|
|
public function updateLocationID($id, $location_id) |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
$device = $this->findOrFail($id); |
|
|
|
|
50
|
|
|
$device->location_id = $location_id; |
51
|
|
|
$device->save(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all the devices with the supplied location id |
56
|
|
|
* |
57
|
|
|
* @param int $location_id |
58
|
|
|
* @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
59
|
|
|
*/ |
60
|
|
|
public function getDevicesBasedOnLocation($location_id) |
61
|
|
|
{ |
62
|
|
|
return self::where('location_id', $location_id)->get(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the first device with the supplied location id |
67
|
|
|
* |
68
|
|
|
* @param int $location_id |
69
|
|
|
* @return Device|Illuminate\Database\Eloquent\Model |
70
|
|
|
*/ |
71
|
|
|
public function getFirstDeviceBasedOnLocation($location_id) |
72
|
|
|
{ |
73
|
|
|
return self::where('location_id', $location_id)->first(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: