AttachAction problem with Relations?

When I try to "Attach" a Panel(resource) in Group(resource), I get this error. Group-Model:
namespace App\Models;

use ...

class Group extends Model
{
use HasFactory;

protected ...

public function panels(): HasMany
{
return $this->hasMany(Panel::class, 'group_id', 'id');
}
}
namespace App\Models;

use ...

class Group extends Model
{
use HasFactory;

protected ...

public function panels(): HasMany
{
return $this->hasMany(Panel::class, 'group_id', 'id');
}
}
Panel-Model:
namespace App\Models;

use ...

class Panel extends Model
{
use HasFactory;

protected ...

public function group(): BelongsTo
{
return $this->belongsTo(Group::class, 'group_id', 'id');
}
}
namespace App\Models;

use ...

class Panel extends Model
{
use HasFactory;

protected ...

public function group(): BelongsTo
{
return $this->belongsTo(Group::class, 'group_id', 'id');
}
}
GroupResource:
public static function getRelations(): array
{
return [
RelationManagers\PanelsRelationManager::class,
//
];
}
public static function getRelations(): array
{
return [
RelationManagers\PanelsRelationManager::class,
//
];
}
PanelsRelationManager:
class PanelsRelationManager extends RelationManager
{
protected static string $relationship = 'panels';

public function form(Form $form): Form
{
return PanelResource::form($form);
}

public function table(Table $table): Table
{
return $table
...
->headerActions([
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['title', 'table_name', 'module_name', 'metadata'])
->preloadRecordSelect()
->recordSelectOptionsQuery(fn (Builder $query) => $query->where('lang', $this->getOwnerRecord()->lang))
])
->...
}
}
class PanelsRelationManager extends RelationManager
{
protected static string $relationship = 'panels';

public function form(Form $form): Form
{
return PanelResource::form($form);
}

public function table(Table $table): Table
{
return $table
...
->headerActions([
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['title', 'table_name', 'module_name', 'metadata'])
->preloadRecordSelect()
->recordSelectOptionsQuery(fn (Builder $query) => $query->where('lang', $this->getOwnerRecord()->lang))
])
->...
}
}
SQL-Scheme:
-- ----------------------------
-- Table structure for group
-- ----------------------------
DROP TABLE IF EXISTS `group`;
CREATE TABLE `group` (
`id` int NOT NULL AUTO_INCREMENT,
`-- ...
PRIMARY KEY (`id`) USING BTREE
);

-- ----------------------------
-- Table structure for panel
-- ----------------------------
DROP TABLE IF EXISTS `panel`;
CREATE TABLE `panel` (
`id` int NOT NULL AUTO_INCREMENT,
`group_id` int DEFAULT NULL, -- group(group_id)
-- ...
PRIMARY KEY (`id`) USING BTREE,
);
-- ----------------------------
-- Table structure for group
-- ----------------------------
DROP TABLE IF EXISTS `group`;
CREATE TABLE `group` (
`id` int NOT NULL AUTO_INCREMENT,
`-- ...
PRIMARY KEY (`id`) USING BTREE
);

-- ----------------------------
-- Table structure for panel
-- ----------------------------
DROP TABLE IF EXISTS `panel`;
CREATE TABLE `panel` (
`id` int NOT NULL AUTO_INCREMENT,
`group_id` int DEFAULT NULL, -- group(group_id)
-- ...
PRIMARY KEY (`id`) USING BTREE,
);
So now; why is it looking for groups instead of group?
No description
5 Replies
RixzZ
RixzZ10mo ago
Your method in the Panel model seems to be called 'group' not 'groups'
Cench愛
Cench愛OP10mo ago
When I call it "groups" I get another error.. 😄 Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::getQualifiedRelatedKeyName() Because it's one to many, not many to many. so it's panelS and group. Not panels and groups..
Dennis Koch
Dennis Koch10mo ago
Define the $inverseRelationship on the relation manager as group
Want results from more Discord servers?
Add your server