Adding different pivotData in N:N relationship

My Property and PropertyFeature has N:N relationship. And I have additional pivot table fields ( ‘number’, ‘max number’, and ‘selected_types’ as a json field).
PropertyFeature has feature 'name' and 'type' field as json. In my pivot table I want to multi-select the json data based on the available options from json 'type' field data in PropertyFeature. This i want to do while creating Property Record. I have so far below code in my Property resource file.
Forms\Components\Select::make('property_type_id')
->relationship(name: 'propertyType', titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyCategories', function ($q) use ($get) {
$q->where('property_category_id', $get('property_category_id'));
});
})
->searchable()
->preload()
->live()
->afterStateUpdated(function (Set $set) {
$set('propertyAmenities', null);
})
->required(),

Forms\Components\Select::make('propertyTypeFeatures')
->multiple()
->relationship(titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyTypes', function ($q) use ($get) {
$q->where('property_type_id', $get('property_type_id'));
});
})
->searchable()
->preload()
->required(),
Forms\Components\Select::make('property_type_id')
->relationship(name: 'propertyType', titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyCategories', function ($q) use ($get) {
$q->where('property_category_id', $get('property_category_id'));
});
})
->searchable()
->preload()
->live()
->afterStateUpdated(function (Set $set) {
$set('propertyAmenities', null);
})
->required(),

Forms\Components\Select::make('propertyTypeFeatures')
->multiple()
->relationship(titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyTypes', function ($q) use ($get) {
$q->where('property_type_id', $get('property_type_id'));
});
})
->searchable()
->preload()
->required(),
What approach I should take for making entry in pivot table for each selected PropertyTypeFeatures. And how?
Solution:
I used below code to solve my challenge. I used repeater in BelongsToMany relationship and used dependent select. ``` Forms\Components\Select::make('property_type_id') ->relationship(name: 'propertyType', titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {...
Jump to solution
1 Reply
Solution
Pritbor
Pritbor8mo ago
I used below code to solve my challenge. I used repeater in BelongsToMany relationship and used dependent select.
Forms\Components\Select::make('property_type_id')
->relationship(name: 'propertyType', titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyCategories', function ($q) use ($get) {
$q->where('property_category_id', $get('property_category_id'));
});
})
->searchable()
->preload()
->live()
->afterStateUpdated(function (Set $set) {
$set('propertyAmenities', null);
})
->required(),


Forms\Components\Repeater::make('propertyPropertyTypeFeatures')
->relationship()
->schema([
Forms\Components\Select::make('property_type_feature_id')
->relationship('propertyTypeFeature', 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyTypes', function ($q) use ($get) {
$q->where('property_type_id', $get('../../property_type_id'));
// $q->where('property_type_id', 1);
});
})
->live()
->afterStateUpdated(function (Set $set) {
$set('selected_feature_types', null);
})
->required(),

Forms\Components\TextInput::make('number')
->integer(),

Forms\Components\TextInput::make('max_number')
->integer(),

Forms\Components\Select::make('selected_feature_types')
->multiple()
->options(function (callable $get){
$selectOptions = PropertyTypeFeature::find($get('property_type_feature_id'));
if($selectOptions){
return $selectOptions->available_feature_type;
}
return null;
})
->preload()
->searchable(),
]),
Forms\Components\Select::make('property_type_id')
->relationship(name: 'propertyType', titleAttribute: 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyCategories', function ($q) use ($get) {
$q->where('property_category_id', $get('property_category_id'));
});
})
->searchable()
->preload()
->live()
->afterStateUpdated(function (Set $set) {
$set('propertyAmenities', null);
})
->required(),


Forms\Components\Repeater::make('propertyPropertyTypeFeatures')
->relationship()
->schema([
Forms\Components\Select::make('property_type_feature_id')
->relationship('propertyTypeFeature', 'name', modifyQueryUsing: function (Builder $query, Get $get) {
$query->whereHas('propertyTypes', function ($q) use ($get) {
$q->where('property_type_id', $get('../../property_type_id'));
// $q->where('property_type_id', 1);
});
})
->live()
->afterStateUpdated(function (Set $set) {
$set('selected_feature_types', null);
})
->required(),

Forms\Components\TextInput::make('number')
->integer(),

Forms\Components\TextInput::make('max_number')
->integer(),

Forms\Components\Select::make('selected_feature_types')
->multiple()
->options(function (callable $get){
$selectOptions = PropertyTypeFeature::find($get('property_type_feature_id'));
if($selectOptions){
return $selectOptions->available_feature_type;
}
return null;
})
->preload()
->searchable(),
]),
Want results from more Discord servers?
Add your server