1
votes

Using the Laravel Administrator package and following the documentation at: http://administrator.frozennode.com/docs/fields#setter-option

Dealing with 2 Eloquent models:
- Manufacturer
- ManufacturerModel (a product of the manufacturer)

My goal is to be able to add a setter field to 'edit_fields' on the manufacturers page that will display a drop down of all manufacturers. This field will then be used in a custom action to re-assign all related ManufacturerModels to the Manufacturer selected form the list (in the event of a duplicate or variation in name). This list of Manufacturers will come from the same table as the selected eloquent model to edit.

Is anyone aware of a custom select that can be used here? I have tried the code below, but in either case, i get a text field rather than a drop down.

Custom select: (model config > edit_fields)

're_assign_models_to' => array(
    'setter' => true,
    'title' => 'Re-assign models to',
    'select' => '(:table)',
    'name_field' => 'name',
),

As well as setting the key as an accessor to return all Manufacturer Eloquent models:

model config > edit_fields

'all_manufacturers' => array(
    'setter' => true,
    'title' => 'Re-assign models to',
    'name_field' => 'name',
),

In base model:

public function getAllManufacturers()   { return Manufacturer::all(); }


Screen shot

Thanks! Any help you can provide would be greatly appreciated!

1

1 Answers

1
votes

Got it....

Apparently the enum type can be used with 'options' of a return from eloquent methods.

This works, in case it helps anyone else:

're_assign' => array(
    'setter' => true,
    'title' => 'Re-assign models to',
    'type' => 'enum',
    'options' => Manufacturer::orderBy('name', 'asc')->lists('name', 'id')
),

Also - a custom action will not work, as form data doesn't appear to be passed to custom actions. However, creating a mutator in the model that the setter goes after did work.

public function setReAssignAttribute($manufacturer) { 

    if(empty($manufacturer))
        return false;

    $this->models()->update(['manufacturer_id' => Manufacturer::where('name', $manufacturer)->first()->id]);

}