michielkempen / nova-order-field

A field that adds reordering functionality to your Laravel Nova resource's index using the eloquent-sortable package by Spatie.
MIT License
59 stars 39 forks source link

Custom pivot table not being called #22

Open Gummby opened 4 years ago

Gummby commented 4 years ago

Not sure if I am implementing this package wrong. But it is asking for me to implement sortable on the model not the custom pivot. When doing this an error Illegal operator and value combination. is returned. I've also noticed it does not call the buildSortQuery on the custom pivot model at all for some reason. But it will on the normal Model if I implement the Sortable package on it. On Nova v2.12.0, Sortable is 3.8.0 and 2.0.4 for this package.

alexdpunkt commented 4 years ago

Same issue here, can't get it to work with a pivot model and sorting column in the pivot table.

App\Models\Form

class Form 
{

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function fields(): BelongsToMany
    {
        return $this->belongsToMany(FormField::class, 'form_field_form')->withPivot([
            'sorting',
            'is_required'
        ])->using(FormFieldForm::class)->orderBy('sorting', 'asc');
    }
}

App\Models\FormField (shouldn't need to implement Sortable in this case because it belongs to the pivot)

use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class FormField implements Sortable
{    
    use SortableTrait;

    /**
     * @var array
     */
    public $sortable = [
        'order_column_name' => 'sorting',
        'sort_when_creating' => true,
    ];

Pivot-Model App\Models\Pivots\FormFieldForm

use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class FormFieldForm extends Pivot implements Sortable
{
    use SortableTrait;

    /**
     * @var array
     */
    public $sortable = [
        'order_column_name' => 'sorting',
        'sort_when_creating' => true,
    ];

App\Nova\Form

class Form extends AbstractResource
{

    /**
     * @var string
     */
    public static $model = 'App\Models\Form';

    /**
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [

            BelongsToMany::make(__('Form fields'), 'fields', FormField::class)->fields(function () {
                return [
                    Toggle::make(__('Required field'), 'is_required')->trueLabel(__('Yes'))->falseLabel(__('No'))->showLabels(),          
                ];
            }),
        ];
    }

App\Nova\FormField

use MichielKempen\NovaOrderField\Orderable;
use MichielKempen\NovaOrderField\OrderField;

class FormField 
{

    use Orderable;

    /**
     * @var string
     */
    public static $model = 'App\Models\FormField';

    /**
     * Get the fields displayed by the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            // [...]
            OrderField::make(__('Sorting'), 'id'),
        ];
    }
}

Error on sort button click: Illegal operator and value combination.

ibrahem-kamal commented 3 years ago

i have the same issue , does this package still maintainable ? @michielkempen