fix: 🐛 set default access URL if model method is missing
This commit is contained in:
parent
ebf10411c9
commit
d23397ee19
2 changed files with 20 additions and 17 deletions
|
|
@ -62,7 +62,7 @@ class InputsController extends Controller
|
|||
->when(!empty($validated['exclude']), function ($collection) use ($validated) {
|
||||
return $collection->reject(fn($item) => $item->id == $validated['exclude']);
|
||||
})
|
||||
->when(!empty($validated['search']), function ($collection) use ($validated) {
|
||||
->when(!empty($validated['search']), function ($collection) use ($validated, $field) {
|
||||
return $collection->filter(fn($item) => is_string($value = data_get($item, $field))
|
||||
&& Str::contains(Str::lower($value), Str::lower($validated['search'])));
|
||||
})
|
||||
|
|
@ -100,7 +100,9 @@ class InputsController extends Controller
|
|||
return response()->json(['error' => 'Invalid model class'], 404);
|
||||
}
|
||||
|
||||
$url = $modelClass::getAccessModel($validated['selectedEntry']);
|
||||
$url = (method_exists($modelClass, 'getAccessModel'))
|
||||
? $modelClass::getAccessModel($validated['selectedEntry'])
|
||||
: static::getAccessModel($validated['selectedEntry'], $validated['modelName']);
|
||||
|
||||
$token = Crypt::encryptString(json_encode([
|
||||
'modelName' => $validated['modelName'],
|
||||
|
|
@ -113,6 +115,22 @@ class InputsController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the url to access a specific resource.
|
||||
*
|
||||
* @param integer $modelId Id of the target model.
|
||||
* @param integer $modelName Name of the target model.
|
||||
* @return string
|
||||
*/
|
||||
private function getAccessModel(int $modelId, string $modelName): string
|
||||
{
|
||||
$targetModel = $this->getClassFromTableName($modelName)::getModelsCached()->firstWhere('id', $modelId);
|
||||
if (!$targetModel) {
|
||||
return null;
|
||||
}
|
||||
return route("front.$modelName.show", $targetModel->slug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve some data from specific token.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -289,21 +289,6 @@ class News extends Model implements TranslatableModel
|
|||
TranslationHelper::validateFallbackTranslations($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the url to access a specific resource.
|
||||
*
|
||||
* @param integer $id Id field of the target model.
|
||||
* @return string
|
||||
*/
|
||||
public static function getAccessModel(int $id): string
|
||||
{
|
||||
$targetModel = self::getModelsCached()->firstWhere('id', $id);
|
||||
if (!$targetModel) {
|
||||
return null;
|
||||
}
|
||||
return route("front.news.show", $targetModel->slug);
|
||||
}
|
||||
|
||||
// * RELATIONSHIPS
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue