具有多对多关系的多选下拉列表

问题描述:

在laravel项目中,我创建了三个表,分别为员工团队 employee_teams ."employee_teams"表中有很多对很多的关系,其中外键为 employee_id team_id .

In my laravel project I created three tables as employees, teams and employee_teams. There is many to many relationship in "employee_teams" tables, with the foreign keys of employee_id and team_id.

"employee_teams"表数据库结构

"employee_teams" table DB structure

id |employee_id |team_id

id | employee_id | team_id

在员工"表单中,有一个多选下拉菜单,可帮助为特定员工分配多个团队.

In the Employee form there is a multi-select dropdown which helps to assign multiple teams for the particular employee.

<select name="namedropdown[]" id="namedropdown" class="selectpicker" multiple data-live-search="true">
    <option value="" disabled selected>Nothing selected</option>
         @foreach ($tdropdown as $key => $tdrop)
         <option value="{{$key}}">{{$tdrop}}</option>
         @endforeach
</select>

我想要的是将"team_id"和"employee_id"保存到"employee_teams"表.

这是员工模型

class employee extends Model
{  
    public function teams()
    {
         return $this->belongsToMany(team::class, 'employee_teams');
    }
}

这是团队模型

class team extends Model
{
    public function employees()
    {
         return $this->belongsToMany(employee::class, 'employee_teams');
    }
}

这是employee_team表迁移

Here is employee_team table migration

class CreateEmployeeTeamsTable extends Migration
{
    public function up()
    {
        Schema::create('employee_teams', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('employee_id')->unsigned();
            $table->integer('team_id')->unsigned();
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('team_id')->references('id')->on('teams')->onDelete('cascade');
            $table->timestamps();
        });
    }

这里是员工控制器中的存储功能

Here is store function in employee controller

use App\employee;
use App\team;
use App\employee_team;

// codes

 $employee = new employee();

       $employee->namedropdown = implode(',', $request->input('namedropdown')); //Already save multi-select dropdown ids to the database 

       $employee->save();

 $employee_team = new employee_team();

      $employee->teams()->attach($employee->namedropdown);

      return redirect()->route('employee.index')->with('success','Data Added');
}

错误出现为 SQLSTATE [01000]:警告:1265第1行的'team_id'列的数据被截断了

请帮助我将employee_id和team_id保存到"employee_teams"表中.非常感谢.

Please help me to save employee_id and team_id to the "employee_teams" table. Thank you so much.

工作示例(经过测试):

A working sample (tested):

迁移:2020_06_16_000000_create_employees_table.php

class CreateEmployeesTable extends Migration
{

    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

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

迁移:2020_06_16_000000_create_teams_table.php

class CreateTeamsTable extends Migration
{
    public function up()
    {
        Schema::create('teams', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

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

迁移:2020_06_16_000000_create_employee_team_table.php

class CreateEmployeeTeamTable extends Migration
{
    public function up()
    {
        Schema::create('employee_team', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('employee_id')->nullable();
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->unsignedBigInteger('team_id')->nullable();
            $table->foreign('team_id')->references('id')->on('teams')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

型号:Employee.php

class Employee extends Model
{    
    public function teams()
    {
        return $this->belongsToMany('App\Team');
    }
}

型号:Team.php

class Team extends Model
{    
    public function employees()
    {
        return $this->belongsToMany('App\Employee');
    }
}

路由:routes/web.php:

Route::get('/select_team/{id?}', 'EmployeeController@select_team')->name('employee.select_team');
Route::post('/save_teams/{id?}', 'EmployeeController@save_teams')->name('employee.save_teams');

控制器:EmployeeController.php

use Illuminate\Http\Request;
use App\Team;
use App\Employee;

class EmployeeController extends Controller
{
    public function select_team($employee_id){    
        return view('select_team', ['tdropdown'=>Team::all(), 'employee'=>Employee::find($employee_id)]);
    }

    public function save_teams(Request $request, $employee_id){   
        $employee = Employee::find($employee_id);
        foreach ($request->namedropdown as $team_id){
            $employee->teams()->attach($team_id);
        }

        return redirect()->route('employee.index')->with('success','Data Added');
    }
}

刀片:select_team.blade.php

<!DOCTYPE html>
<form action="/save_teams/{{$employee->id}}" method="post">
        @csrf
    <select name="namedropdown[]" id="namedropdown" class="selectpicker" multiple data-live-search="true">
    <option value="" disabled selected>Nothing selected</option>
            @foreach ($tdropdown as $tdrop)
            <option value="{{$tdrop->id}}">{{$tdrop->name}}</option>
            @endforeach
    </select>
    <button>Submit</button>
</form>
</html>

我认为这是一个可以为您提供想法的好例子.

I think it's a good example that can give you an idea.