mardi 4 décembre 2018

Eloquent: guard ID when inserting into one database but don't guard it for another database.

I need to guard the ID column when inserting into a database, however I don't want to guard it when inserting into a different database due to needing to manually set the ID, so that the tables are in sync.

However I can't figure out a way to do it, below is what I have got at the moment, however this doesn't work at all as I just get an error:

Field 'id' doesn't have a default value

This is my current model:

<?php

namespace App\Models\Seasonal;

use Illuminate\Database\Eloquent\Model;

class SeasonalBanner extends Model
{

    protected $connection = 'dev';
    protected $guarded = [ 'id' ];
    protected $appends = [ 'period' ];

    public static function boot()
    {
        parent::boot();

        self::creating(function($model){
            if ($model->connection === 'live') {
                $model->guarded = [];
            }
        });
    }

    public function dates() {
        return $this->hasMany(SeasonalBannerDates::class);
    }

    public function getPeriodAttribute() {
        return [ $this->start, $this->end ];
    }

}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2E1Zn4x
via IFTTT

Aucun commentaire:

Enregistrer un commentaire