Laravel по-русски

Русское сообщество разработки на PHP-фреймворке Laravel.

Ты не вошёл. Вход тут.

#1 19.12.2018 18:51:57

Вставка в 2 таблицы с отношениями и связями foreign_key

есть 2 модели Slidestructs и Slideimages, таблицы связаны так: [Slidestructs].[slideimages_id] = [Slideimages].[id]

Но slidestructs.slideimages_id значение не увеличивается, может ошибка в hasOne ?

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSlidestructsTable extends Migration
{
    public function up()
    {
        Schema::create('slidestructs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('slideimages_id')->unsigned();

            $table->integer('number')->unsigned();
            $table->string('title', 2000);
            $table->integer('title_sign');
            $table->string('category', 2000);
            $table->string('url_image', 2000);
            $table->string('url_thumb', 2000);
            $table->integer('thumb_position')->nullable();
            $table->string('content');

            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('slidestructs');
    }
}
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSlideimagesTable extends Migration
{
    public function up()
    {
        Schema::create('slideimages', function (Blueprint $table) {
            $table->increments('id');
            $table->string('url', 2000)->default('');
            $table->timestamps();
        });

      Schema::table('slidestructs', function($table) {
        $table->foreign('slideimages_id')->references('id')->on('slideimages');
      });

    }

    public function down()
    {
        Schema::dropIfExists('slideimages');
    }
}

В контроллере:

        $slidestruct = new Slidestruct();

        $slidestruct->slideimages_id = '1';
        $slidestruct->number = '1';
        $slidestruct->title = 'заголовок 2';
        $slidestruct->title_sign = '0';
        $slidestruct->category = 'cat 2';
        $slidestruct->url_image = '[url]http://image.jpg[/url]';
        $slidestruct->url_thumb = '[url]http://thumb.jpg[/url]';
        $slidestruct->thumb_position = '0';
        $slidestruct->content = 'image';

        $slideimage = new Slideimage();
        $slideimage->url = '[url]http://test.jpg[/url]';

        $slideimage_id = Slideimage::create(json_decode($slideimage, true));
        $slideimage_test = $slideimage_id->slidestruct()->create(json_decode($slidestruct, true));

модель Slideimage:

  public function slidestruct()
  {
      return $this->belongsTo('App\Slidestruct', 'slideimages_id');
  }

модель Slidestruct:

  public function slideimage()
  {
      return $this->hasOne('App\Slideimage', 'slideimages_id', 'id');
  }

Не в сети

Подвал раздела