首页 文章

将数据插入表pivot使用表单并在laravel 5.7中显示index.blade.php

提问于
浏览
0

我在下面创建了team.blade.php

form.blade.php

表单组index.blade.php

index.blade.php

dd

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

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

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

my user models

<?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');
    }
    }

Team Models

<?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);
}

}

Project Model

<?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);
}
}

UserTeam model

<?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);
}
}

Team Controller

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 . 我怎么克服这个?

1 回答

  • 0

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

    Example from the docs:

    Attaching

    Eloquent还提供了一些额外的辅助方法,使相关模型的使用更加方便 . 例如,假设用户可以拥有多个角色,而角色可以拥有许多用户 . 要通过在连接模型的中间表中插入记录来将角色附加到用户,请使用attach方法:

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

    In your case:

    Team Controller

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

相关问题