@@ -105,16 +105,16 @@ public function getRequest()
105105 *
106106 * @param string $model_namespace Full model namespace. Ex: App\Models\Article
107107 *
108- * @throws \ Exception in case the model does not exist
108+ * @throws Exception in case the model does not exist
109109 */
110110 public function setModel ($ model_namespace )
111111 {
112112 if (! class_exists ($ model_namespace )) {
113- throw new \ Exception ('The model does not exist. ' , 500 );
113+ throw new Exception ('The model does not exist. ' , 500 );
114114 }
115115
116116 if (! method_exists ($ model_namespace , 'hasCrudTrait ' )) {
117- throw new \ Exception ('Please use CrudTrait on the model. ' , 500 );
117+ throw new Exception ('Please use CrudTrait on the model. ' , 500 );
118118 }
119119
120120 $ this ->model = new $ model_namespace ();
@@ -198,7 +198,7 @@ public function setRoute($route)
198198 * @param string $route Route name.
199199 * @param array $parameters Parameters.
200200 *
201- * @throws \ Exception
201+ * @throws Exception
202202 */
203203 public function setRouteName ($ route , $ parameters = [])
204204 {
@@ -207,7 +207,7 @@ public function setRouteName($route, $parameters = [])
207207 $ complete_route = $ route .'.index ' ;
208208
209209 if (! \Route::has ($ complete_route )) {
210- throw new \ Exception ('There are no routes for this route name. ' , 404 );
210+ throw new Exception ('There are no routes for this route name. ' , 404 );
211211 }
212212
213213 $ this ->route = route ($ complete_route , $ parameters );
@@ -348,7 +348,7 @@ public function sync($type, $fields, $attributes)
348348 * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If
349349 * the provided length is negative, then that many relations will be omitted from the end of the relation
350350 * string.
351- * @param \Illuminate\Database\Eloquent\ Model $model Optionally specify a different model than the one in the crud object.
351+ * @param Model $model Optionally specify a different model than the one in the crud object.
352352 * @return string Relation model name.
353353 */
354354 public function getRelationModel ($ relationString , $ length = null , $ model = null )
@@ -380,7 +380,7 @@ public function getRelationModel($relationString, $length = null, $model = null)
380380 * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from
381381 * the many addresses of the company of a given user).
382382 *
383- * @param \Illuminate\Database\Eloquent\ Model $model Model (eg: user).
383+ * @param Model $model Model (eg: user).
384384 * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given
385385 * Model or one from a different Model through multiple relations. A dot notation can be used to specify
386386 * multiple relations (eg: user.company.address).
@@ -392,6 +392,7 @@ public function getRelatedEntriesAttributes($model, $relationString, $attribute)
392392 $ endModels = $ this ->getRelatedEntries ($ model , $ relationString );
393393 $ attributes = [];
394394 foreach ($ endModels as $ model => $ entries ) {
395+ /** @var Model $model_instance */
395396 $ model_instance = new $ model ();
396397 $ modelKey = $ model_instance ->getKeyName ();
397398
@@ -430,7 +431,7 @@ public function getRelatedEntriesAttributes($model, $relationString, $attribute)
430431 /**
431432 * Parse translatable attributes from a model or models resulting from the specified relation string.
432433 *
433- * @param \Illuminate\Database\Eloquent\ Model $model Model (eg: user).
434+ * @param Model $model Model (eg: user).
434435 * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model).
435436 * @param string $value Attribute value translatable or not
436437 * @return string A string containing the translated attributed based on app()->getLocale()
@@ -446,7 +447,7 @@ public function parseTranslatableAttributes($model, $attribute, $value)
446447 }
447448
448449 if (! is_array ($ value )) {
449- $ decodedAttribute = json_decode ($ value , true );
450+ $ decodedAttribute = json_decode ($ value , true ) ?? ( $ value !== null ? [ $ value ] : []) ;
450451 } else {
451452 $ decodedAttribute = $ value ;
452453 }
@@ -462,11 +463,26 @@ public function parseTranslatableAttributes($model, $attribute, $value)
462463 return $ value ;
463464 }
464465
466+ public function setLocaleOnModel (Model $ model )
467+ {
468+ $ useFallbackLocale = $ this ->shouldUseFallbackLocale ();
469+
470+ if (method_exists ($ model , 'translationEnabled ' ) && $ model ->translationEnabled ()) {
471+ $ locale = $ this ->getRequest ()->input ('_locale ' , app ()->getLocale ());
472+ if (in_array ($ locale , array_keys ($ model ->getAvailableLocales ()))) {
473+ $ model ->setLocale (! is_bool ($ useFallbackLocale ) ? $ useFallbackLocale : $ locale );
474+ $ model ->useFallbackLocale = (bool ) $ useFallbackLocale ;
475+ }
476+ }
477+
478+ return $ model ;
479+ }
480+
465481 /**
466482 * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending
467483 * associated model instance or instances.
468484 *
469- * @param \Illuminate\Database\Eloquent\ Model $model The CRUD model.
485+ * @param Model $model The CRUD model.
470486 * @param string $relationString Relation string. A dot notation can be used to chain multiple relations.
471487 * @return array An array of the associated model instances defined by the relation string.
472488 */
0 commit comments