jeudi 25 octobre 2018

How to insert in a translatable field?

Using Lumen 5.1, I'd like to know how to to CRUD on a translatable field, in my case it's name.

This is migration file

class CreateDomainHolidays extends Migration
{

    protected $table = 'domain_holidays';
    protected $app_table = true;

    public function up()

    {
        Schema::create($this->getTable(), function (Blueprint $table) {
            $table->increments('id');

            $table->integer('domain_id')->unsigned()->nullable();
            $table->foreign('domain_id')->references('id')
                ->on(self::getTableName('domains'))->onDelete('cascade');
            $table->string('name')->nullable()->default('')->translatable = true;
            $table->dateTime('start_date');
            $table->dateTime('end_date');
            $table->tinyInteger('half_day_start')->default(0);
            $table->tinyInteger('half_day_end')->default(0);
            $this->parenttable = $table;

        });
        $this->createTableWithTranslation($this->parenttable);

    }

    public function down()
    {
        $this->dropTranslationTable($this->getTable());
        Schema::drop($this->getTable());
    }
}

This is my model

 class Holiday extends BaseModel
    {
        protected $table = 'domain_holidays';
        protected $app_table = true;
        public $timestamps = false;

        protected $translation = true;
        public function translations()
        {
            return $this->hasMany('App\Models\General\HolidayTranslations', 'parent_id');
        }

    }

    class HolidayTranslations extends BaseTranslationModel
    {
        protected $table = 'domain_holidays_i18n';
        protected $primaryKey = 'translation_id';
    }
}

domain_holidays contains

id
domain_id
start_date
end_date
half_day_start
half_day_end

domain_holidays_i18n contains

translation_id
parent_id
lang
name

Something like this is not working

public static function setHoliday($domainId, $name, $startDate, $endDate, $halfDayStart, $halfDayEnd)
{
    Holiday::unguard();
    return Holiday::create([
        'domain_id' => $domainId,
        'name' => $name,
        'start_date' => $startDate,
        'end_date' => $endDate,
        'half_day_start' => $halfDayStart,
        'half_day_end' => $halfDayEnd
    ]);
}

Postman would return an error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list'



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire