gpt4 book ai didi

mysql - 如何插入到数据透视表

转载 作者:行者123 更新时间:2023-11-29 19:44:53 24 4
gpt4 key购买 nike

我正在尝试使用 Laravel 5.3 和 mysql 跟踪半私有(private)网球类(class)记录。我正在尝试遵循 Laravel 5 多对多教程中设置的示例。我有一个玩家模型,我试图通过 Player_Sharedlessonhours 数据透视表与 Sharedlessonhours 表同步。在代码中,我的共享类(class)表获得插入的记录,但数据透视表没有。抱歉说得这么啰嗦,但我想弄清楚发生了什么。

首先是表结构。

     public function up()
{
Schema::create('sharedlessonhours', function (Blueprint $table) {
$table->increments('id');
$table->date('signup_date');
$table->integer('packages_id')->unsigned();
$table->timestamps();


});

Schema::table('sharedlessonhours', function (Blueprint $table) {
$table->foreign('packages_id')->references('id')->on('packages');
});


Schema::create('player_sharedlessonhour', function (Blueprint $table)
{
$table->integer('sharedlessonhours_id')->unsigned();
$table->integer('players_id')->unsigned();
$table->timestamps();


});

Schema::table('player_sharedlessonhour', function (Blueprint $table) {
$table->foreign('sharedlessonhours_id')->references('id')->on('sharedlessonhours')->onDelete('cascade');
$table->foreign('players_id')->references('id')->on('players')->onDelete('cascade');

});
}

创建和存储方法:

    public function createSharedLessonhours()
{
$players = Players::orderBy('lname')->pluck('fname', 'id');
$packages = Packages::orderBy('name')->pluck('name','id');
return view('admin.lessonhours.sharedlessonhours', compact('players', 'packages'));
}

public function storeSharedLessonhours(Request $request)
{
$sharedlessonhours = SharedLessonhours::create($request->all());

$sharedlessonhours->players()->attach($request->input('players'));
}

表格:

        <div class="col-md-4 col-sm-5">
{!! Form::open(['url' => 'sharedlessonhours']) !!}
<div class="form-group">
{!! Form::label('players', 'Player(s):') !!}
{!! Form::select('players[]', $players, null, ['class' => 'form-control', 'multiple']) !!}
</div>
<div class="form-group">
{!! Form::label('signup_date', 'Signup Date:') !!}
{!! Form::text('signup_date', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('packages_id', 'Lesson Package:') !!}
{!! Form::select('packages_id', $packages, null, ['class' => 'form-control', 'placeholder' => 'Choose Package']) !!}
</div>

<div class="form-group">
{!! Form::submit('Signup', ['class' => 'btn btn-default form-control']) !!}
</div>

{!! Form::close() !!}
</div>

玩家 ID 选择框的屏幕截图: Screenshot of select box

错误截图: SQLError Screenshot

我想知道数组的顺序是否不正确? dd($request) 显示所有内容都是从表单中收集的,但查看错误页面似乎正在尝试将数据插入到错误的字段中。如果这对 mysql 很重​​要,那么包含 (3,4) 的行将不是正确的顺序。我尝试翻转mysql中的列,但没有帮助。我现在很困惑。

更新:首先,我将分享请求的代码,然后分享我采取的进一步调试步骤。

玩家型号代码:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Collective\Html\Eloquent\FormAccessible;
use Carbon\Carbon;


class Players extends Model
{
public $table = "players";

protected $fillable = array('fname', 'lname', 'gender', 'birthdate');


public function users()
{
return $this->belongsTo('App\User', 'users_id');
}

public function lessonHours()
{
return $this->hasMany('App\Lessonhours', 'players_id');
}

public function sharedlessonhours()
{
return $this->belongsToMany('App\SharedLessonhours', "player_sharedlessonhour","players_id", "sharedlessonhours_id" );
}

public function getFullName($id)
{
return ucfirst($this->fname ) . ' ' . ucfirst($this->lname);
}

protected $dates = ['birthdate'];

public function setBirthdateAttribute($value)
{
$this->attributes['birthdate'] = Carbon::createFromFormat('m/d/Y', $value);
}
}

共享类(class)时间:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Collective\Html\Eloquent\FormAccessible;
use Carbon\Carbon;

class SharedLessonhours extends Model
{
use FormAccessible;

protected $fillable = array('signup_date', 'packages_id');

public $table = "sharedlessonhours";

public function players()
{
return $this->belongsToMany('App\Players', "player_sharedlessonhour", "players_id", "sharedlessonhours_id");
}

public function sharedhoursused()
{
return $this->hasMany('App\SharedHoursused', 'id');
}

public function packages()
{
return $this->belongsTo('App\Packages');
}

public function setSignUpDateAttribute($value)
{
$this->attributes['signup_date'] = Carbon::createFromFormat('m/d/Y', $value);
}
}

我尝试在更改 mysql 中的列后添加不同的记录,但再次出现错误。我将列切换回来,即使重新启动修补程序后,错误仍然存​​在。

tinker screenshot

enter image description here

最佳答案

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to

来自 Laravel 多对多关系文档。

因此,您的 SharedLessonhour 模型中 players 关系的定义应为

public function players()
{
return $this->belongsToMany('App\Players', "player_sharedlessonhour", "sharedlessonhours_id", "players_id");
}

尝试更改定义并查看。我还没有测试过,但这似乎是违反约束的原因。

关于mysql - 如何插入到数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com