Миграция
class CreateModelCompatibilityTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('model_compatibility', function (Blueprint $table) {
$table->increments('id');
$table->string('mark');
$table->string('market');
$table->string('model');
$table->string('modif');
$table->string('date');
$table->string('shortsAll')->unique();
$table->string('url');
$table->timestamps();
});
Schema::create('prso_products_model_compatibility', function (Blueprint $table) {
$table->integer('model_id')->unsigned()->index();
$table->foreign('model_id')->references('id')->on('model_compatibility')->onDelete('cascade');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('prso_products')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('model_compatibility');
Schema::drop('prso_products_model_compatibility');
}
}
Модели и отношения
class PrsoProduct extends Model
{
protected $table = 'prso_products';
protected $primaryKey = 'id';
protected $fillable = [
'category_id', 'name', 'slug', 'price', 'description', 'number','status', 'artikul', 'views', 'show', 'ostatok'
];
public function models()
{
return $this->belongsToMany(ModelCompatibility::class)->withPivot('id');
}
}
И
class ModelCompatibility extends Model
{
protected $table = 'model_compatibility';
protected $primaryKey = 'id';
protected $fillable = [
'mark', 'market', 'model', 'modif', 'date', 'shortsAll','url'
];
public function product()
{
return $this->belongsToMany(PrsoProduct::class)->withPivot('id');
}
}
метод в контроллере
public function ParseCategory()
{
$prod = new Product();
$ParseProds = $prod->offset(6)->limit(4)->get();
$Model = new ModelCompatibility();
$mark = ['nissan', 'infiniti'];
foreach ($mark as $k => $m) {
$NISMarkets = SearchNum::getNisMarkets($k);
}
foreach ($ParseProds as $parseProd) {
$number = $parseProd->number;
reset($NISMarkets);
while (list($mark, $v) = each($NISMarkets)) {
$parseSearch = $this->searchNISNumber($number, $mark);
if ($parseSearch->get('errors') == null) {
$insert = [];
$Model->updateOrCreate(['shortsAll' => $shortsAll], $insert);
$ParseProds->models()->attach($Model);/// ВОТ ТАК ВОТ НЕ ЗАПИСЫВАЕТ
continue;
}
}
}
}