33namespace Kroderdev \LaravelMicroserviceCore \Models ;
44
55use Illuminate \Database \Eloquent \Model as BaseModel ;
6+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
67use Illuminate \Pagination \LengthAwarePaginator ;
78use Illuminate \Support \Collection ;
89use Illuminate \Support \Str ;
@@ -79,6 +80,18 @@ public static function find($id, $columns = ['*']): ?self
7980 return static ::fromResponse ($ data );
8081 }
8182
83+ /**
84+ * Find a model by its primary key or throw an exception.
85+ */
86+ public static function findOrFail ($ id , $ columns = ['* ' ]): self
87+ {
88+ if ($ model = static ::find ($ id , $ columns )) {
89+ return $ model ;
90+ }
91+
92+ throw (new ModelNotFoundException ())->setModel (static ::class, [$ id ]);
93+ }
94+
8295 /**
8396 * Paginate models from the API.
8497 */
@@ -103,6 +116,75 @@ public static function create(array $attributes = []): ?self
103116 return static ::fromResponse ($ data );
104117 }
105118
119+ /**
120+ * Update a model via the API.
121+ *
122+ * @param int|string $id The ID of the model to update.
123+ * @param array $attributes The attributes to update.
124+ * @return bool
125+ */
126+ public static function updateById ($ id , array $ attributes ): bool
127+ {
128+ $ method = strtolower (config ('microservice.models.update_method ' , 'put ' ));
129+ $ response = static ::client ()->{$ method }(static ::endpoint ().'/ ' .$ id , $ attributes );
130+
131+ if (is_object ($ response ) && method_exists ($ response , 'successful ' )) {
132+ return $ response ->successful ();
133+ }
134+
135+ return true ;
136+ }
137+
138+ /**
139+ * Update the model via the API.
140+ */
141+ public function update (array $ attributes = [], array $ options = []): bool
142+ {
143+ if (! $ this ->exists ) {
144+ return false ;
145+ }
146+
147+ $ this ->fill ($ attributes );
148+
149+ $ method = strtolower (config ('microservice.models.update_method ' , 'put ' ));
150+ $ response = static ::client ()->{$ method }(static ::endpoint ().'/ ' .$ this ->getKey (), $ this ->attributesToArray ());
151+
152+ $ data = static ::parseResponse ($ response );
153+ if ($ fresh = static ::fromResponse ($ data )) {
154+ $ this ->fill ($ fresh ->attributesToArray ());
155+ $ this ->syncOriginal ();
156+ }
157+
158+ return true ;
159+ }
160+
161+ /**
162+ * Update the model via the API, throwing an exception on failure.
163+ */
164+ public function updateOrFail (array $ attributes = [], array $ options = []): bool
165+ {
166+ if (! $ this ->exists ) {
167+ return false ;
168+ }
169+
170+ $ this ->fill ($ attributes );
171+
172+ $ method = strtolower (config ('microservice.models.update_method ' , 'put ' ));
173+ $ response = static ::client ()->{$ method }(static ::endpoint ().'/ ' .$ this ->getKey (), $ this ->attributesToArray ());
174+
175+ if (is_object ($ response ) && method_exists ($ response , 'successful ' ) && ! $ response ->successful ()) {
176+ throw new \RuntimeException ('Update failed. ' );
177+ }
178+
179+ $ data = static ::parseResponse ($ response );
180+ if ($ fresh = static ::fromResponse ($ data )) {
181+ $ this ->fill ($ fresh ->attributesToArray ());
182+ $ this ->syncOriginal ();
183+ }
184+
185+ return true ;
186+ }
187+
106188 /**
107189 * Save the model via the API.
108190 */
0 commit comments