morphToMany issue with RelationManager

Hi, i have a problem with the relation manager when i try to associate a Product Model to a Order Model, the query give me an error "Column not found: 1054 Unknown column 'products.order_id' in 'field list'" what am I doing wrong? Thanks This is my model code:
class Order extends Model
{
public function products()
{
return $this->morphedByMany(Product::class, 'order_item');
}
}
class Order extends Model
{
public function products()
{
return $this->morphedByMany(Product::class, 'order_item');
}
}
class Product extends Model implements Purchasable
{
public function orders()
{
return $this->morphToMany(Order::class, 'order_item');
}

}
class Product extends Model implements Purchasable
{
public function orders()
{
return $this->morphToMany(Order::class, 'order_item');
}

}
class OrderItem extends Model
{
public function order_item()
{
return $this->morphTo();
}
}
class OrderItem extends Model
{
public function order_item()
{
return $this->morphTo();
}
}
class ProductsRelationManager extends RelationManager
{
protected static string $relationship = 'products';

protected static ?string $inverseRelationship = 'orders';

public function form(Form $form): Form
{
return $form
->schema([]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('order_id')
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
//
])
->headerActions([
Tables\Actions\AssociateAction::make()->form(fn (AssociateAction $action): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('name')
->options(Product::all()->pluck('name', 'id'))
->required(),
]),
])
->actions([
Tables\Actions\DissociateAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DissociateBulkAction::make(),
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}


}
class ProductsRelationManager extends RelationManager
{
protected static string $relationship = 'products';

protected static ?string $inverseRelationship = 'orders';

public function form(Form $form): Form
{
return $form
->schema([]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('order_id')
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
//
])
->headerActions([
Tables\Actions\AssociateAction::make()->form(fn (AssociateAction $action): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('name')
->options(Product::all()->pluck('name', 'id'))
->required(),
]),
])
->actions([
Tables\Actions\DissociateAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DissociateBulkAction::make(),
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}


}
1 Reply
psycho_cfh
psycho_cfh7mo ago
This is the query with error
select `products`.`order_id`, `products`.`id` from `products` inner join `order_items` on `products`.`id` = `order_items`.`order_item_id` where (`products`.`order_id` like %f%) and not exists (select * from `orders` inner join `order_items` on `orders`.`id` = `order_items`.`order_id` where `products`.`id` = `order_items`.`order_item_id` and `order_items`.`order_item_type` = App\Models\Product and `orders`.`id` = 2) order by `products`.`order_id` asc limit 50
select `products`.`order_id`, `products`.`id` from `products` inner join `order_items` on `products`.`id` = `order_items`.`order_item_id` where (`products`.`order_id` like %f%) and not exists (select * from `orders` inner join `order_items` on `orders`.`id` = `order_items`.`order_id` where `products`.`id` = `order_items`.`order_item_id` and `order_items`.`order_item_type` = App\Models\Product and `orders`.`id` = 2) order by `products`.`order_id` asc limit 50
the problem i think is order by products.order_id asc limit 50