Laravel Pivot Table вставка данных - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть три таблицы.

Table Diagramm

Я хочу добавить oyuncu (плеер) в группу (группу), поэтому я создал метод (публичная функция oyuncustore(Request $request, $grup_id)).Но я получаю сообщение об ошибке и не могу добавить строки в сводную таблицу.

Ошибка:

ErrorException в строке BelongsToMany.php 866: аргумент 1 передан в Illuminate \ Database \Eloquent \ Relations \ BelongsToMany :: formatSyncList () должен иметь массив типов, заданную строку, вызываемый в ......... / vendor / laravel / framework / src / Illuminate / Database / Eloquent / Relations / BelongsToMany.php на линии 831 и определен

Grup.php

class Grup extends Model
{

    public function turnuva(){

        return $this->belongsTo('App\Turnuva');
    }

    public function oyuncular(){

        return $this->belongsToMany('App\Oyuncu');

   }
}

Turnuva.php

class Turnuva extends Model
{
    protected $table ='turnuvas';

    public function grups(){

        return $this->hasMany('App\Grup');
    }
}

Oyuncu.php

class Oyuncu extends Model
{
     protected $table ='oyuncular';
     //$fillable = ['grup_id', 'oyuncu_id'];

      public function grups(){

        return $this->belongstoMany('App\Grup');
    }

}

сводная таблица: grup_oyuncu создан следующим образом:

class CreateGrupOyuncuTable extends Migration
{
    public function up()
    {
        Schema::create('grup_oyuncu', function (Blueprint $table) {

            $table->increments('id');
            $table->integer('oyuncu_id')->unsigned();
            $table->foreign('oyuncu_id')->references('id')->on('oyuncular')->onDelete('CASCADE')->onUpdate('CASCADE');

            $table->integer('grup_id')->unsigned();
            $table->foreign('grup_id')->references('id')->on('grups')->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->engine = 'InnoDB';
           });
    }


    public function down()
    {
        Schema::table('grup_oyuncu', function (Blueprint $table) {
       //$table->dropForeign(['user_id']);
        $table->dropForeign('grup_oyuncu_grup_id_foreign');
        $table->dropForeign('grup_oyuncu_oyuncu_id_foreign');
    });

        Schema::drop('grup_oyuncu');

    }
}

GrupController:

public function oyuncustore(Request $request, $grup_id)
    {

    $gr = new Grup;

    $tumoyuncular = Oyuncu::All();
    $grup= Grup::find($grup_id);

    $gr->oyuncular()->sync($request->oyuncu, false);

    $grpoyuncular = DB::table('grup_oyuncu')->select('oyuncu_id')->get();   
    Session::flash('success','Gruba oyuncu eklendi.');

    return view('gruplar.show')->withGrup($grup)->withTumoyuncular($tumoyuncular)->withGrpoyuncular($grpoyuncular);

}

Форма:

{!! Form::open(['route'=>['gruplar.oyuncustore',$grup->id],'method'=>'POST'])!!}

            <h2>Yeni Oyuncu Ekle</h2>

            {{Form::label('oyuncu','Oyuncu Adı Soyadı:')}}

            <select class="form-control" name="oyuncu" id="">
            @foreach($tumoyuncular as $toyuncu)

            <option value="{{ $toyuncu->id}} ">{{$toyuncu->ad}} {{$toyuncu->soyad}}</option>
            @endforeach

        </select>


            {{Form::submit('Oyuncu Ekle', array('class'=>'btn btn-success btn-lg btn-block', 'style'=>'margin-top:20px;'))}}


            {!! Form::close()!!}

Может быть, я неправильно создал отношения с таблицами?

...