当前位置: 首页 > 知识库问答 >
问题:

在表pivot use form中插入数据,并在laravel 5.7中显示index.blade.php

谢志用
2023-03-14

我在下面创建team.blade.php

表单Team index.blade.php

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

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

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

我的用户模型

<?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\u idteam\u id字段。我如何克服这个问题??

共有1个答案

全心思
2023-03-14

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

文档中的示例:

附加

雄辩还提供了一些额外的助手方法,使使用相关模型更加方便。举个例子,我们想象一个用户可以有很多角色,一个角色可以有很多用户。若要通过在连接模型的中间表中插入记录将角色附加到用户,请使用附加方法:

$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');
}
 类似资料:
  • 我的数据确实显示在console.log中,但实际上没有显示在表中,我在这里做错了什么?

  • 我是一个初学者一般反应和前端开发。这里有一个显示表格的“简单”代码。 错误如下: 警告:列表中的每个孩子都应该有一个唯一的“键”道具。 检查。看见https://banned-fb.example.com/react-warning-keys了解更多信息。在TableDataRow(在Table.js:30)在TableBody(在Table.js:44)在Table(在Table.js:42)在

  • 问题内容: 我在Angular中显示JSON数据时遇到麻烦。我已成功将数据从后端发送到前端(角度),但无法显示它们。 我已经尝试在JSFiddle上模拟类似的情况,尽管我已经从后端准备了数据 获取/发送数据->后端: 获取数据->前端(角度) 带有ng-repeat指令的HTML部分: 谁知道是什么问题吗? 问题答案: 据我所知,您将对象存储为JSON,但从未对其进行解析。因此使用 代替 应该解决

  • 问题内容: 我有2个表:product和cart,我希望结合这2个表并根据特定条件以数组形式显示数据,如下所示: 应显示特定类别下的所有产品,如果特定用户购买了给定产品中的任何产品,则其详细信息也应显示在该产品的前面 我到目前为止所做的代码是 它给出了这样的数组 但是我想要代替上面的数组的最后一个数组是 产品表视图(如您所见,产品表中包含一个productid,在每个productid下最多可以有

  • 问题内容: 我想在SQL表中表示列表“ hi”,“ hello”,“再见”,“早安”,“ howdy”(按此顺序): “ pk”是主键列。忽略其值。 “ i”是“索引”,用于定义“ val”列中值的顺序。它 仅 用于建立顺序,否则值并不重要。 我遇到的问题是在保持顺序的同时将值插入到列表中。例如,如果我想插入“嘿”,并且希望它出现 在 “你好”和“再见”之间,则我必须将“再见”和“早安”的“ i”

  • 问题内容: 我正在将数据插入MySQL数据库,以防万一如果插入失败,我需要给出一条适当的错误消息来指示。根据下面的代码,如果插入成功或失败,我将收到消息。 我要做的是仅在数据插入成功时才回显消息,如果失败则返回。我该如何修改我的代码? 问题答案: