Closed PrafullaKumarSahu closed 5 years ago
Hello the optionsResolve need to return a collection with ID and NAME
Example:
->optionsResolve(function ($company) { return $company->departments()->get(['id', 'name']); })
@orlyapps thank you for your quick reply, I have also tried that, but still, I am getting id and not building_number.
NovaBelongsToDepend::make('Customer')
->options(\App\Models\Customer::all()),
NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Quote')
->optionsResolve(function ($customer) {
// Reduce the amount of unnecessary data sent
return $customer->addresses()->get(['id', 'building_number']);
})
->dependsOn('Customer'),
This is the code.
NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Quote')
Is this really correct? Or can ist be:
NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Address')
@orlyapps I am getting anempty select option for NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Address')
.
I have 3 models, Customer -> having Address (hasMany-belongsTo
)
and Customer->having Quote (hasMany-belongsTo
), but for creating quote, I want to store, for which address of the customer, this quote is created, also defined the hasManyThrough relationship for Quote and Address, but here, I am getting addresses from $customer, so based on selection of customer I am displaying address, and it seems working very well, but not displaying option value in select box and when saving the quote, getting that error "This customer addresses may not be associated with this resource.?" , Am I describing the problem correctly, or you need some more data or code, please let me know.
@orlyapps Can you please let me know if I am doing anything wrong here?
@orlyapps It has been three days and I am unable to get it correct, in tinker, I am able to get the properties correctly
>>> App\Models\Customer::first()->addresses()->get(['id', 'building_number'])
=> Illuminate\Database\Eloquent\Collection {#3132
all: [
App\Models\Address {#3125
id: 1,
building_number: "45",
},
],
}
>>>
but in form, it is coming only id, also, I would like to know why it is showing
This customer addresses may not be associated with this resource.?
when I am trying to submit the form?
@orlyapps As Address will be related to Quote directly, I removed the hasManyThrough relationship now,
class Quote extends Model
{
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function address()
{
return $this->belongsTo(Address::class);
}
}
and Customer
class Customer extends Model
{
public function addresses()
{
return $this->hasMany(Address::class);
}
public function quotes()
{
return $this->hasMany(Quote::class);
}
}
and Address model has
class Address extends Model { public function customer() { return $this->belongsTo(Customer::class, 'customer_id', 'id'); } } and now it is working fine, thank you.
Not using resource title as option options, displaying id
Customer and Address shares hasMany-belongsTo relationship.
In Address
public static $title = 'building_number';
but why it is showingid
in options alsowhen saving the quote, getting error "This customer addresses may not be associated with this resource."? @orlyapps Can you suggest, what I am doing wrong?