У меня есть следующие модели Eloquent со связями «многие ко многим»:
class User extends Model
{
public function products()
{
return $this->belongsToMany(Product::class)
->using('App\UserProductPivot')
->withPivot('label', 'is_primary', 'sort_order')
}
}
class Product extends Model
{
public $incrementing = false;
protected $casts = [
'id' => "string"
];
}
И, как показано в отношении продукта пользователя, я использую пользовательскую модель промежуточной таблицы «App \ UserProductPivot»:
<?php namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserStationPivot extends Pivot {
protected $casts = ['product_id' => 'string'];
}
Миграция сводной таблицы:
Schema::create('station_user', function (Blueprint $table) {
$table->integer('user_id');
$table->string('product_id');
$table->boolean('is_primary')->nullable();
$table->string('label')->nullable();
$table->integer('sort_order');
$table->primary(['user_id', 'product_id']);
$table->timestamps();
});
Проблема
Внутри метода пользовательской модели:
$array = [23456 => [
"is_primary" => true
"label" => "Test"
"sort_order" => 1
],
"138seq" => [
"is_primary" => false
"label" => "Test"
"sort_order" => 2
]];
$this->products()->sync($array);
При заданной настройке я ожидал, что «product_id» 23456 будет приведен к строке, но при вызове sync () это приведет к ошибке SQL в поле product_id, поскольку он не был приведен к строке. Что происходит неправильно?