It seems like you're trying to dynamically create relationships and form fields in Filament based on tags from your database. There are a few issues in your code that need to be addressed.
Firstly, in your __call method, you're trying to dynamically handle method calls that don't exist on the model. However, the createRatingMethod should return a relationship instance, but it's not clear if it's being used correctly since the __call method should return the result of the relationship method, not the relationship method itself.
Secondly, in your Ratings method within the hobby resource, you're using a foreach loop but returning on the first iteration, which means you're only creating a select field for the first rating. You should instead be collecting all the select fields and returning them as an array.
Here's a revised version of your code:
In your Hobby model:
public function __call($method, $parameters)
{
// Check if the method exists as a tag in the Tag table
$tag = Tag::where('tag', $method)->first();
if ($tag) {
return $this->morphToMany(Tag::class, 'taggable')
->where('tag_type', 13)
->where('tag', $tag->tag)
->withTimestamps();
}
return parent::__call($method, $parameters);
}
In your Hobby resource:
public static function Ratings()
{
$ratings = Tag::where('tag_type', 13)
->where('parent_id', null)
->orderBy('tag', 'asc')
->get();
$fields = [];
foreach ($ratings as $rating) {
$fields[] = Select::make($rating->tag)
->label($rating->tag)
->relationship(
name: $rating->tag,
titleAttribute: 'tag'
);
}
return $fields;
}
And in your form schema:
Section::make('Ratings')
->schema([
...self::Ratings(), // Spread operator to include all fields
]),
Make sure to use the spread operator (...) to include all the fields in the schema array.
Regarding the error "Call to a member function getResults() on null", this suggests that the relationship method you're trying to call does not exist or is not returning a proper relationship instance. Ensure that the dynamic method names you're creating match the actual relationship names and that they return valid relationship instances.
Lastly, if you're still having trouble with saving the data, you might need to look into how Filament handles saving relationships and ensure that your dynamic fields are correctly configured to save the related data. You may need to override the save method in your Filament resource or use a custom Livewire component to handle the saving logic.