Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom pivot model: order_column doesn't have a default value #66

Closed
esl51 opened this issue Oct 30, 2018 · 5 comments
Closed

Custom pivot model: order_column doesn't have a default value #66

esl51 opened this issue Oct 30, 2018 · 5 comments

Comments

@esl51
Copy link

esl51 commented Oct 30, 2018

Hello! I can't make work this behavior with custom pivot model.
I have models:

class Questionnaire extends Model
{
    use HasTranslations;

    protected $fillable = [
        'title', 'is_default', 'type', 'company_id'
    ];
    public $translatable = ['title'];

    public function attributes()
    {
        return $this->belongsToMany(Attribute::class, 'questionnaire_attribute')->using(QuestionnaireAttribute::class);
    }
}

class Attribute extends Model
{
    use HasTranslations;

    protected $fillable = [
        'name', 'title', 'type', 'icon', 'is_notify_filter'
    ];
    public $translatable = ['title'];

    public function questionnaires()
    {
        return $this->belongsToMany(Questionnaire::class, 'questionnaire_attribute')->using(QuestionnaireAttribute::class);
    }
}

class QuestionnaireAttribute extends Pivot implements Sortable
{
    use HasTranslations, SortableTrait;

    protected $table = 'questionnaire_attribute';
    public $translatable = ['title'];

    public function questionnaire()
    {
        return $this->belongsTo(Questionnaire::class);
    }

    public function attribute()
    {
        return $this->belongsTo(Attribute::class);
    }
}

And when try to attach, I get this error:

SQLSTATE[HY000]: General error: 1364 Field 'order_column' doesn't have a default value (SQL: insert into `questionnaire_attribute` (`attribute_id`, `is_clearable`, `is_required`, `questionnaire_id`, `title`) values (1, 0, 1, 1, {"en":"Test"}))

Laravel 5.7, MariaDB 10.3.9

@esl51
Copy link
Author

esl51 commented Oct 30, 2018

This event not firing on Pivot.

@freekmurze
Copy link
Member

I believe we can't solve this error in this package.

If you do know a good solution, feel free to PR it.

@bakerkretzmar
Copy link

Just wanted to share that I had the same question and I'm pretty sure I have it working. Laravel 5.8 added model events for pivot table models, so that event does fire now.

@esl51 it looks like you just didn't create the order_column column on your pivot table in the DB. This should work:

class CreateQuestionnaireAttributeTable extends Migration
{
    public function up()
    {
        Schema::create('questionnaire_attribute', function (Blueprint $table) {
            // ...
            $table->integer('order_column');
        });
    }
}

class Questionnaire extends Model
{
    public function attributes()
    {
        return $this->belongsToMany(Attribute::class, 'questionnaire_attribute')
                    ->using(QuestionnaireAttribute::class);
    }
}

class Attribute extends Model
{
    public function questionnaires()
    {
        return $this->belongsToMany(Questionnaire::class, 'questionnaire_attribute')
                    ->using(QuestionnaireAttribute::class);
    }
}

class QuestionnaireAttribute extends Pivot implements Sortable
{
    use SortableTrait;

    public $incrementing = true;
}

Now if you attach() any questionnaires and attributes to each other, the order column should be filled in correctly.

@Sylk
Copy link

Sylk commented Nov 26, 2019

Now if you attach() any questionnaires and attributes to each other, the order column should be filled in correctly.

Hey @bakerkretzmar I'm just getting the following error following with what you've posted.

Call to undefined method App\Http\Controllers\BookController::fromRawAttributes()

@pokono
Copy link

pokono commented Feb 3, 2020

On attach works fine, but it seems to loose references when calling moveOrderUp(), moveOrderUp(). etc.

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: update `course_module` set `order_index` = 2, `course_module`.`updated_at` = 2020-02-03 05:22:21 where `` = 56 and `` = 316)

The two where are missing foreignKey & relatedKey.

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants