0

Есть модель машин:

Schema::create('cars', function (Blueprint $table) {
   $table->collation = 'utf8_general_ci';
   $table->charset = 'utf8';

   $table->increments('id');
   $table->string('number',15)->unique();
   $table->enum('body', ['sedan', 'hatchback', 'universal', 'minivan', 
       'microbus',]);
   $table->boolean('climat');

   $table->string('color',25)->nullable();
   $table->foreign('color')->references('title')->on('colors');

   $table->integer('model_id')->unsigned()->nullable();
   $table->foreign('model_id')->references('id')->on('models');

   $table->engine = 'InnoDB';
});

и модель цвет

Schema::create('colors', function (Blueprint $table) {
  $table->collation = 'utf8_general_ci';
  $table->charset = 'utf8';

  $table->string('title',25)->primary('title');
  $table->string('ru_title',25);
  $table->string('ua_title',25);
  $table->string('en_title',25);

  $table->engine = 'InnoDB';
});

в цвете есть связь

public function cars() {
  return $this->hasMany('App\Models\Car', 'color', 'title');
}

в машинах

public function color() {
    return $this->belongsTo('App\Models\Color', 'car_id', 'id');
}

$color->cars вызывается, а $car->color - нет. Подскажите, пожалуйста, что неправильно.

1 ответ 1

0

'car_id', 'id' - не соответствуют связке таблиц. данные связываются по полям title и color

думаю в машинах можно использовать

public function color() {
    return $this->hasOne('App\Models\Color', 'title', 'color')
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.