Relation Manager and HasManyThrough: General error: 1 no such column on one of the models

Hello everyone, I'm building a library management system using filament and I'm trying to add relation manager to handle the intermediate tables that I need but I must be doing something wrong... The error message is: SQLSTATE[HY000]: General error: 1 no such column: chapters.chapter_story_id I have a Chapter defined like this: migration:
public function up(): void
{
Schema::create('chapters', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
public function up(): void
{
Schema::create('chapters', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
model:
class Chapter extends Model
{
use HasFactory;

protected $fillable = ['name'];

public function stories(): HasManyThrough
{
return $this->hasManyThrough(Story::class, ChapterStory::class);
}
}
class Chapter extends Model
{
use HasFactory;

protected $fillable = ['name'];

public function stories(): HasManyThrough
{
return $this->hasManyThrough(Story::class, ChapterStory::class);
}
}
And a Story defined like this: migration:
public function up(): void
{
Schema::create('stories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->foreignIdFor(Language::class);
$table->timestamps();
});
}
public function up(): void
{
Schema::create('stories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->foreignIdFor(Language::class);
$table->timestamps();
});
}
model
class Story extends Model
{
use HasFactory;

protected $fillable = ["name", "description", "language_id"];

public function books(): BelongsToMany
{
return $this->belongsToMany(Book::class);
}

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

public function chapters(): HasManyThrough
{
return $this->hasManyThrough(Chapter::class, ChapterStory::class);
}
}
class Story extends Model
{
use HasFactory;

protected $fillable = ["name", "description", "language_id"];

public function books(): BelongsToMany
{
return $this->belongsToMany(Book::class);
}

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

public function chapters(): HasManyThrough
{
return $this->hasManyThrough(Chapter::class, ChapterStory::class);
}
}
And I have an intermediate table called ChapterStory: migration:
public function up(): void
{
Schema::create('chapter_stories', function (Blueprint $table) {
$table->id();
$table->integer('order');
$table->foreignIdFor(Chapter::class);
$table->foreignIdFor(Story::class);
$table->timestamps();
});
}
public function up(): void
{
Schema::create('chapter_stories', function (Blueprint $table) {
$table->id();
$table->integer('order');
$table->foreignIdFor(Chapter::class);
$table->foreignIdFor(Story::class);
$table->timestamps();
});
}
model:
class ChapterStory extends Model
{
use HasFactory;

protected $fillable = ['order', 'chapter_id', 'story_id'];

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

public function story(): BelongsTo
{
return $this->belongsTo(Story::class);
}
}
class ChapterStory extends Model
{
use HasFactory;

protected $fillable = ['order', 'chapter_id', 'story_id'];

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

public function story(): BelongsTo
{
return $this->belongsTo(Story::class);
}
}
I haven't modified the relation manager:
class ChaptersRelationManager extends RelationManager
{
protected static string $relationship = 'chapters';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
class ChaptersRelationManager extends RelationManager
{
protected static string $relationship = 'chapters';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Am I doing something wrong ?
2 Replies
CORONEL
CORONEL4mo ago
Your relationships is wrong. You don't need hasManyThrough, you structure looks to me like a belongsToMany. Get a grip on that first... That's Laravel 101, nothing to do with Filament.
Matteo G
Matteo G4mo ago
Thanks for the reply, CORONEL! I'm pretty new to Laravel, so that may be right. I'll check that more thoroughly and come back with more info or a solution 🙂 ok, managed to get something working; Chapter -> belongsToMany Story using a custom pivot table (ChapterStory) Story -> hasManyThrough Chapter using ChapterStory This way the relation manager picks up on the chapters correctly. I only have a problem using the ChapterStoryResource that has to do with the redirection that happens after a record is created: Missing required parameter for [Route: filament.admin.resources.chapter-stories.edit] [URI: admin/chapter-stories/{record}/edit] [Missing parameter: record]. Creating the resource works fine and clicking on the table rows redirects to the correct edit view, the error only happens on create. Is this normal when extending the Pivot Eloquent class ? got it! belongsToMany on both tables xD but it doesn't solve the creation step redirection 😦
Want results from more Discord servers?
Add your server