1 | <?php |
||
11 | trait EloquentJsQueries |
||
12 | { |
||
13 | /** |
||
14 | * Scope to results that satisfy the string-encoded list of query methods described by $stack. |
||
15 | * |
||
16 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
17 | * @param string $stack |
||
18 | */ |
||
19 | public function scopeUseEloquentJs($query, $stack = null) |
||
29 | |||
30 | /** |
||
31 | * Handle dynamic calls to scope methods. |
||
32 | * |
||
33 | * Yes, this is indeed a scope for calling another scope method. |
||
34 | * While there are other ways we could support scopes, this has |
||
35 | * the benefit of requiring no special treatment in our BaseQueryTranslator |
||
36 | * - no different from how we handle `where`, `orderBy`, etc. |
||
37 | * |
||
38 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
39 | * @param string $name |
||
40 | * @param array $parameters |
||
41 | */ |
||
42 | public function scopeScope($query, $name, $parameters = []) |
||
53 | |||
54 | /** |
||
55 | * Prepare a date for array / JSON serialization. |
||
56 | * |
||
57 | * Overrides the date serializer to ensure our Javascript can |
||
58 | * understand the format. |
||
59 | * |
||
60 | * @todo implement date handling without global side effects |
||
61 | * @param DateTime $date |
||
62 | * @return string |
||
63 | */ |
||
64 | protected function serializeDate(DateTime $date) |
||
68 | |||
69 | /** |
||
70 | * Return a timestamp as DateTime object. |
||
71 | * |
||
72 | * Accept dates from Javascript in any format that Carbon recognises. |
||
73 | * |
||
74 | * @param string $value |
||
75 | * @return Carbon |
||
76 | */ |
||
77 | protected function asDateTime($value) |
||
85 | |||
86 | /** |
||
87 | * Get the endpoint defined on this model. |
||
88 | * |
||
89 | * @return string|null |
||
90 | */ |
||
91 | public function getEndpoint() |
||
99 | } |
||
100 |
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: