To dynamically get a list of UserDefinedAttributes and assign them to $customAttrs in the PublicationStoreRequest class, you can create a method in the UserDefinedAttributes model that retrieves the custom attributes. Here's an example of how you can modify the code:
use App\Models\UserDefinedAttributes;
class PublicationStoreRequest extends FormRequest
{
// ...
public function rules()
{
$commonAttrs = [
'title' => 'required|string|max:255',
'publicationType' => 'required|string',
'authors' => 'required|array',
'publicationDate' => 'required|date',
// etc
];
$customAttrs = UserDefinedAttributes::pluck('attribute_name')
->mapWithKeys(function ($attribute) {
return [$attribute => 'nullable|string|max:255'];
})
->toArray();
$expectedAttrs = array_merge($commonAttrs, $customAttrs);
return $expectedAttrs;
}
// ...
}
In this example, we use the pluck method to retrieve all the attribute names from the UserDefinedAttributes model. Then, we use the mapWithKeys method to transform the attribute names into an associative array where the attribute name is the key and the validation rule is the value. Finally, we convert the collection to an array using the toArray method and merge it with the common attributes.
Make sure to import the UserDefinedAttributes model at the top of the file:
use App\Models\UserDefinedAttributes;
This solution assumes that the UserDefinedAttributes model has a column named attribute_name that stores the names of the custom attributes. Adjust the code accordingly if your model has a different column name.
Remember to replace the UserDefinedAttributes model with the actual namespace and class name of your UserDefinedAttributes model.