1 | <?php |
||
12 | trait EloquentJsQueries |
||
13 | { |
||
14 | /** |
||
15 | * Scope to results that satisfy an EloquentJs query. |
||
16 | * |
||
17 | * @param Builder $query |
||
18 | * @param string|null|callable $rules |
||
19 | */ |
||
20 | public function scopeEloquentJs(Builder $query, $rules = null) |
||
30 | |||
31 | /** |
||
32 | * Handle dynamic calls to scope methods. |
||
33 | * |
||
34 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
35 | * @param string $name |
||
36 | * @param array $parameters |
||
37 | */ |
||
38 | public function scopeScope($query, $name, $parameters = []) |
||
49 | |||
50 | /** |
||
51 | * Prepare a date for array / JSON serialization. |
||
52 | * |
||
53 | * Overrides the date serializer to ensure our Javascript can |
||
54 | * understand the format. |
||
55 | * |
||
56 | * @todo implement date handling without global side effects |
||
57 | * @param DateTime $date |
||
58 | * @return string |
||
59 | */ |
||
60 | protected function serializeDate(DateTime $date) |
||
64 | |||
65 | /** |
||
66 | * Return a timestamp as DateTime object. |
||
67 | * |
||
68 | * Accept dates from Javascript in any format that Carbon recognises. |
||
69 | * |
||
70 | * @param string $value |
||
71 | * @return Carbon |
||
72 | */ |
||
73 | protected function asDateTime($value) |
||
81 | |||
82 | /** |
||
83 | * Get the endpoint defined on this model. |
||
84 | * |
||
85 | * @return string|null |
||
86 | */ |
||
87 | public function getEndpoint() |
||
95 | } |
||
96 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: