我正在尝试使用Laravel 5.3和mysql跟踪半私人网球课程记录。我试图按照Laravel 5多对多教程中的示例设置。我有一个玩家模型,我试图通过Player_Sharedlessonhours数据透视表与Sharedlessonhours表同步。在代码中,我的sharedlessonhours表获取插入的记录,但数据透视表没有。很抱歉这么详细,但我想清楚发生了什么。
首先是表格结构。
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>
我想知道数组是否只是错误的顺序? 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);
}
}
SharedLessonhours:
<?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中的列后添加不同的记录并再次获取错误。在重新启动修补程序之后,我将列重新切换回来并继续错误。
答案 0 :(得分:0)
除了自定义连接表的名称之外,您还可以通过将其他参数传递给belongsToMany方法来自定义表上键的列名。第三个参数是您定义关系的模型的外键名称,而第四个参数是您要加入的模型的外键名称
来自多对多关系的laravel文档。
因此,players
模型中SharedLessonhour
关系的定义应为
public function players()
{
return $this->belongsToMany('App\Players', "player_sharedlessonhour", "sharedlessonhours_id", "players_id");
}
尝试更改定义并查看。我没有测试,但这似乎是违反约束的原因。