Am i misunderstanding relation managers?
Hi all,
I keep getting the below error
When trying to attach a product to my category through my CategoryResource relation manager.
Can anyone give me advise I can't see where i'm going wrong - sorry for the inconvenience.
Call to undefined method App\Models\Product::categories()
Call to undefined method App\Models\Product::categories()
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'sku',
'description',
'category_id',
'created_at',
'updated_at'
];
public function category()
{
return $this->belongsTo(Category::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'sku',
'description',
'category_id',
'created_at',
'updated_at'
];
public function category()
{
return $this->belongsTo(Category::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'created_at',
'updated_at'
];
public function products()
{
return $this->hasMany(Product::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'created_at',
'updated_at'
];
public function products()
{
return $this->hasMany(Product::class);
}
}
3 Replies
<?php
namespace App\Filament\Resources\CategoryResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ProductsRelationManager extends RelationManager
{
protected static string $relationship = 'products';
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('sku')
->required()
->maxLength(255),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('sku')
->columns([
Tables\Columns\TextColumn::make('sku'),
Tables\Columns\TextColumn::make('description'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
<?php
namespace App\Filament\Resources\CategoryResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ProductsRelationManager extends RelationManager
{
protected static string $relationship = 'products';
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('sku')
->required()
->maxLength(255),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('sku')
->columns([
Tables\Columns\TextColumn::make('sku'),
Tables\Columns\TextColumn::make('description'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Give this page a thorough read. With a belongsTo you need to use an AssociateAction. https://filamentphp.com/docs/3.x/panels/resources/relation-managers
Sorted it thank you