1 | <?php |
||
7 | class Link extends Model |
||
8 | { |
||
9 | /** |
||
10 | * The attributes that are mass assignable. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $fillable = [ |
||
15 | 'url', |
||
16 | 'hash', |
||
17 | 'user_id', |
||
18 | ]; |
||
19 | |||
20 | /** |
||
21 | * Generate a link with a unique hash. |
||
22 | * |
||
23 | * @see generateUniqueHash |
||
24 | * |
||
25 | * @param string $url |
||
26 | * @param User|null $user |
||
27 | * |
||
28 | * @return mixed |
||
29 | * |
||
30 | * @internal param int $user_id |
||
31 | */ |
||
32 | public function generate($url = '', User $user = null) |
||
33 | { |
||
34 | $hash = $this->generateUniqueHash(); |
||
35 | $data = [ |
||
36 | 'url' => $url, |
||
37 | 'hash' => $hash, |
||
38 | ]; |
||
39 | |||
40 | /* |
||
41 | * If the user_id has been offered |
||
42 | * add the user_id so we can build |
||
43 | * a relationship between link and |
||
44 | * user. |
||
45 | */ |
||
46 | if ($user) { |
||
47 | $data['user_id'] = $user->id; |
||
|
|||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Finally insert the link into |
||
52 | * the database. |
||
53 | */ |
||
54 | $link = self::create($data); |
||
55 | |||
56 | return $link; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Create a hash for this link to use. |
||
61 | * |
||
62 | * @return int |
||
63 | */ |
||
64 | private function generateUniqueHash() |
||
74 | |||
75 | /** |
||
76 | * Return the user for this link. |
||
77 | * |
||
78 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
79 | */ |
||
80 | public function user() |
||
84 | |||
85 | /** |
||
86 | * Return the hits for this link. |
||
87 | * |
||
88 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
89 | */ |
||
90 | public function hits() |
||
94 | } |
||
95 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.