我有多对多关系,可以正确插入数据,但是当我尝试获取数据时,它没有给我任何数据。
一张桌子是老板,另一张是工人
Migration
<?php
public function up()
{
Schema::create('boss_worker', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('worker_id');
$table->unsignedBigInteger('boss_id');
$table->timestamps();
});
}
老板型号关系
public function workers()
{
return $this->belongsToMany(\App\Models\Admin\Worker::class,'boss_worker');
}
我如何尝试获取数据
public function index()
{
$boss = Boss::find(1);
dd($boss->workers);
}
我如何插入数据
$input = $request->all();
$workers = $input['workers'];
$input['workers'] = implode(',', $workers);
$boss = Boss::where('staff_id',$request->boss)->first();
$worker_gangs = $boss->workers()->sync($workers);
它没有获取任何数据