@@ 144-177 (lines=34) @@ | ||
141 | * |
|
142 | * @return Model|object |
|
143 | */ |
|
144 | protected function instantiateApiResourceModel(string $type) |
|
145 | { |
|
146 | try { |
|
147 | // Try Eloquent model factory |
|
148 | ||
149 | // Factories are usually defined without the leading \ in the class name, |
|
150 | // but the user might write it that way in a comment. Let's be safe. |
|
151 | $type = ltrim($type, '\\'); |
|
152 | ||
153 | return factory($type)->make(); |
|
154 | } catch (\Exception $e) { |
|
155 | if (Flags::$shouldBeVerbose) { |
|
156 | echo "Eloquent model factory failed to instantiate {$type}; trying to fetch from database.\n"; |
|
157 | } |
|
158 | ||
159 | $instance = new $type(); |
|
160 | if ($instance instanceof \Illuminate\Database\Eloquent\Model) { |
|
161 | try { |
|
162 | // we can't use a factory but can try to get one from the database |
|
163 | $firstInstance = $type::first(); |
|
164 | if ($firstInstance) { |
|
165 | return $firstInstance; |
|
166 | } |
|
167 | } catch (\Exception $e) { |
|
168 | // okay, we'll stick with `new` |
|
169 | if (Flags::$shouldBeVerbose) { |
|
170 | echo "Failed to fetch first {$type} from database; using `new` to instantiate.\n"; |
|
171 | } |
|
172 | } |
|
173 | } |
|
174 | } |
|
175 | ||
176 | return $instance; |
|
177 | } |
|
178 | ||
179 | /** |
|
180 | * @param array $tags |
@@ 67-100 (lines=34) @@ | ||
64 | * |
|
65 | * @return Model|object |
|
66 | */ |
|
67 | protected function instantiateTransformerModel(string $type) |
|
68 | { |
|
69 | try { |
|
70 | // try Eloquent model factory |
|
71 | ||
72 | // Factories are usually defined without the leading \ in the class name, |
|
73 | // but the user might write it that way in a comment. Let's be safe. |
|
74 | $type = ltrim($type, '\\'); |
|
75 | ||
76 | return factory($type)->make(); |
|
77 | } catch (Exception $e) { |
|
78 | if (Flags::$shouldBeVerbose) { |
|
79 | echo "Eloquent model factory failed to instantiate {$type}; trying to fetch from database.\n"; |
|
80 | } |
|
81 | ||
82 | $instance = new $type(); |
|
83 | if ($instance instanceof IlluminateModel) { |
|
84 | try { |
|
85 | // we can't use a factory but can try to get one from the database |
|
86 | $firstInstance = $type::first(); |
|
87 | if ($firstInstance) { |
|
88 | return $firstInstance; |
|
89 | } |
|
90 | } catch (Exception $e) { |
|
91 | // okay, we'll stick with `new` |
|
92 | if (Flags::$shouldBeVerbose) { |
|
93 | echo "Failed to fetch first {$type} from database; using `new` to instantiate.\n"; |
|
94 | } |
|
95 | } |
|
96 | } |
|
97 | } |
|
98 | ||
99 | return $instance; |
|
100 | } |
|
101 | ||
102 | /** |
|
103 | * @param array $tags |