【问题标题】:insert data to table pivot use form and show it index.blade.php in laravel 5.7在 laravel 5.7 中将数据插入表格数据透视表并显示 index.blade.php
【发布时间】:2018-10-19 07:24:16
【问题描述】:

我在下面有表单 create team.blade.php

Form Team index.blade.php

与该用户一起显示一个团队用户的名称,并显示该用户正在完成的项目,并将该用户显示为什么(角色)。

一个用户的关系有多个团队。一个团队有很多用户。因此,我选择多对多关系。但是当我创建团队时,我想在透视 user_teams 表中插入 user_id 和 team_id 作为用户和团队之间的关系表。

但是当我尝试创建团队失败时,他没有将数据保存到 user_teams 表中。 并且在团队索引中,他不显示该用户的团队用户名称,而是显示该用户正在做的项目,并将用户显示为什么。

我的用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Presence;
use App\Models\Project;
use App\Productivity;
use App\Sick_leave;
use App\Annual_leave;
use App\Models\Team;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'role_id',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function role()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }

public function teams()
    {
        return $this->belongsToMany(Team::class, 'user_teams');
    }

public function projects()
    {
        return $this->belongsToMany(Project::Class, 'user_projects');
    }
    }

团队模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\User;
use App\Role;
use Auth;

class Team extends Model
{
use SoftDeletes;

protected $table = 'teams';
protected $fillable = [
    'id', 
    'project_id',  
];

public function users()
{
    return $this->belongsToMany(User::class, 'user_teams');
}

public function project()
{
    return $this->belongsTo(Project::class);
}

public function role()
{
    return $this->belongsTo(Role::class);
}

}

项目模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Project extends Model
{
use SoftDeletes;
protected $table = 'projects';
protected $fillable = [
    'project_id',
    'project_name',
    'start_date',
    'end_date',
    'project_category',
];

public function users()
{
    return $this->belongsToMany(User::class, 'user_projects');
}

public function team()
{
    return $this->hasOne(Team::class);
}
}

用户团队模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserTeam extends Model
{
use SoftDeletes;    
protected $table = "user_teams";

public function team()
{
    return $this->belongsTo(Team::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}
}

团队主管

public function index()
{
    $users = auth()->user()->name;
    $users = User::all();
    return view ('teams.index', compact ('teams', 'users', 'projects'));
}
public function create()
{
    $users = User::all();
    $projects = Project::pluck('project_name', 'id');
    return view ('teams.form', compact('projects', 'users'));
}
public function store(Request $request)
{
    $team = Team::create($request->all());
    $userIds = User::find(2);
    $team->users()->attach($userIds);
    return redirect()->route('team.create');
}

在 user_teams 中有字段 user_idteam_id。我该如何克服呢??

【问题讨论】:

    标签: laravel eloquent many-to-many


    【解决方案1】:

    您必须创建团队,然后附加属于它的用户。

    来自docs 的示例:

    附加

    Eloquent 还提供了一些额外的辅助方法来使工作 搭配相关机型更方便。例如,让我们想象一个用户 可以有很多角色,一个角色可以有很多用户。附加角色 通过在连接的中间表中插入一条记录给用户 模型,使用 attach 方法:

    $user = App\User::find(1);
    
    $user->roles()->attach($roleId);
    

    在你的情况下:

    团队主管

    public function store(Request $request)
    {
        $team = Team::create($request->except('user_id'));
        $team->users()->attach($request->get('user_id', []));
        return redirect()->route('team.create');
    }
    

    【讨论】:

    • 桌队一定要加user_id吗?
    • 不,你的架构是正确的,Laravel 会为你创建带有team_id user_id 的数据透视表,你只需要将用户的id 传递给 attach 方法。跨度>
    • 我试了一下,他进入了数据透视表,但是对于user_id,他与根据选择的选项选择的user_id不匹配。
    • 您必须将您在创建表单中选择的 users 传递给 store 操作,此时您仅使用 2 的 id 附加用户。
    • store 函数中。对不起
    猜你喜欢
    • 2019-03-26
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 2021-11-15
    • 2020-09-19
    相关资源
    最近更新 更多