orlyapps / nova-belongsto-depend

Larave Nova BelongsTo Field with Dependcy
MIT License
182 stars 65 forks source link

Displaying id instead of title of resource #42

Closed PrafullaKumarSahu closed 5 years ago

PrafullaKumarSahu commented 5 years ago

Not using resource title as option options, displaying id

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;
                })
                ->dependsOn('Customer')

Customer and Address shares hasMany-belongsTo relationship.

In Address public static $title = 'building_number'; but why it is showing id in options also

when saving the quote, getting error "This customer addresses may not be associated with this resource."? @orlyapps Can you suggest, what I am doing wrong?

orlyapps commented 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']); })

PrafullaKumarSahu commented 5 years ago

@orlyapps thank you for your quick reply, I have also tried that, but still, I am getting id and not building_number.

PrafullaKumarSahu commented 5 years ago
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.

orlyapps commented 5 years ago

NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Quote') Is this really correct? Or can ist be: NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Address')

PrafullaKumarSahu commented 5 years ago

@orlyapps I am getting anempty select option for NovaBelongsToDepend::make('Address', 'customerAddresses', 'App\Nova\Resources\Address').

PrafullaKumarSahu commented 5 years ago

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.

PrafullaKumarSahu commented 5 years ago

@orlyapps Can you please let me know if I am doing anything wrong here?

PrafullaKumarSahu commented 5 years ago

@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?

PrafullaKumarSahu commented 5 years ago

@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.