F
Filament7mo ago
Kaan

Call to undefined method App\Models\Category::category_image()

Hi. I'm using this package for the media library: https://filamentphp.com/plugins/awcodes-curator In my CategoryResource, i'm trying to insert a form data with image. Here's my CategoryResource > Form Method:
public static function form(Form $form): Form
{
return $form
->schema([
Wizard::make([
Wizard\Step::make('Language')
->description('Select the language for this category')
->schema([
Forms\Components\Select::make('language_id')
->label('Language')
->options(Language::all()->where('is_active', true)->pluck('name', 'id'))
->required(),
]),
Wizard\Step::make('About Category')
->description('Enter the details of the category')
->schema([
Forms\Components\Select::make('parent_id')
->label('Parent Category')
->helperText('If there is no parent category, leave this field empty')
->options(function (callable $get) {
$language = Language::find($get('language_id'));
if (!$language) {
return Category::where('id', null)->pluck('name', 'id');
}
return Category::where('language_id', $language->id)->pluck('name', 'id');
})
->searchable()
->placeholder('Select parent category')
->default(null),
CuratorPicker::make('category_image_id')
->relationship('category_image', 'id'),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Textarea::make('description')
->maxLength(65535),
Forms\Components\Toggle::make('is_active')
->required()
->default(true),
]),
]),
])->columns(1);
}
public static function form(Form $form): Form
{
return $form
->schema([
Wizard::make([
Wizard\Step::make('Language')
->description('Select the language for this category')
->schema([
Forms\Components\Select::make('language_id')
->label('Language')
->options(Language::all()->where('is_active', true)->pluck('name', 'id'))
->required(),
]),
Wizard\Step::make('About Category')
->description('Enter the details of the category')
->schema([
Forms\Components\Select::make('parent_id')
->label('Parent Category')
->helperText('If there is no parent category, leave this field empty')
->options(function (callable $get) {
$language = Language::find($get('language_id'));
if (!$language) {
return Category::where('id', null)->pluck('name', 'id');
}
return Category::where('language_id', $language->id)->pluck('name', 'id');
})
->searchable()
->placeholder('Select parent category')
->default(null),
CuratorPicker::make('category_image_id')
->relationship('category_image', 'id'),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Textarea::make('description')
->maxLength(65535),
Forms\Components\Toggle::make('is_active')
->required()
->default(true),
]),
]),
])->columns(1);
}
And here's my Category Model:
class Category extends Model
{
use HasFactory, Sluggable;

protected $fillable = [
'language_id',
'name',
'slug',
'description',
'parent_id',
'order',
'is_active',
'category_image_id',
];

public function sluggable(): array
{
return [
'slug' => [
'source' => 'name'
]
];
}


public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}

public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}

public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id');
}

public function products(): HasMany
{
return $this->hasMany(Product::class);
}

public function scopeActive($query)
{
return $query->where('is_active', true);
}

public function scopeOrdered($query)
{
return $query->orderBy('order', 'asc');
}

public function categoryImage(): BelongsTo
{
return $this->belongsTo(Media::class, 'category_image_id', 'id');
}
}
class Category extends Model
{
use HasFactory, Sluggable;

protected $fillable = [
'language_id',
'name',
'slug',
'description',
'parent_id',
'order',
'is_active',
'category_image_id',
];

public function sluggable(): array
{
return [
'slug' => [
'source' => 'name'
]
];
}


public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}

public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}

public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id');
}

public function products(): HasMany
{
return $this->hasMany(Product::class);
}

public function scopeActive($query)
{
return $query->where('is_active', true);
}

public function scopeOrdered($query)
{
return $query->orderBy('order', 'asc');
}

public function categoryImage(): BelongsTo
{
return $this->belongsTo(Media::class, 'category_image_id', 'id');
}
}
When i try to insert data in my CategoryResource i'm getting this error:
Call to undefined method App\Models\Category::category_image()
Call to undefined method App\Models\Category::category_image()
How can i solve the problem?
Filament
Curator by Adam Weston - Filament
A media manager / picker plugin for Filament Panels.
1 Reply
awcodes
awcodes7mo ago
try this
CuratorPicker::make('category_image_id')
->relationship('categoryImage', 'id'),
CuratorPicker::make('category_image_id')
->relationship('categoryImage', 'id'),
nothing obvious is standing out in your code to me though. this is working just fine in my demo app
CuratorPicker::make('featured_image_id')
->relationship('featured_image', 'id'),


public function featured_image(): BelongsTo
{
return $this->belongsTo(Media::class, 'featured_image_id', 'id');
}
CuratorPicker::make('featured_image_id')
->relationship('featured_image', 'id'),


public function featured_image(): BelongsTo
{
return $this->belongsTo(Media::class, 'featured_image_id', 'id');
}
Want results from more Discord servers?
Add your server