Laravel Pivot表的许多关系,没有Laravel惯例

发布于 2025-02-10 10:33:25 字数 2059 浏览 1 评论 0原文

我有2个模型用户团队,每个团队都可以拥有多个用户,每个用户都可以在任何团队中,因此我想建立许多与许多关系。

我正在尝试自定义某些东西,我不想将Laravel惯例用于枢轴表。

这是我的用户迁移:

 class CreateUsersTable extends Migration
{
    protected $collection = "table_users";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('username')->unique();
                    $table->string('password');
             }
    }

}

这是我的团队迁移:

 class CreateTeamTable extends Migration
{
    protected $collection = "table_team";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('name')->unique();
             }
    }

}

所以我的问题是我想在用户模型和team模型中创建关系的方法。

在团队模型中:

    public function users()
    {
        return $this->belongsToMany(User::class, '??', '??' , '??'); // what should I insert in '??'
    }

在用户模型中:

    public function teams()
    {
        return $this->belongsToMany(Team::class, '??', '??' , '??'); // what should I insert in '??'
    }

以及如何在不使用任何约定的情况下创建枢轴表:

我到目前为止创建的枢轴表:

class CreateUsersTeamTable extends Migration
{
    protected $collection = "team_user_pivot";
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(  $this->collection
            , function (Blueprint $table) {
            $table->id();
            $table->string('??');  // please help me to fill this field
            $table->string('??');  // please help me to fill this field
            $table->timestamps();
        });
    }

'??'??' in 用户模型和团队模型和枢轴表。帮我填补每个人

I have 2 model user and team, every team can have multiple users and each user can be in any team , so I wanna make a many to many relation.

I'm trying to customize something , I don't wanna use the laravel convention for pivot table.

Here is my User Migration:

 class CreateUsersTable extends Migration
{
    protected $collection = "table_users";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('username')->unique();
                    $table->string('password');
             }
    }

}

And here is my Team Migration :

 class CreateTeamTable extends Migration
{
    protected $collection = "table_team";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('name')->unique();
             }
    }

}

So my problem is when I want to create method for the relation in User Model and Team Model.

in Team Model :

    public function users()
    {
        return $this->belongsToMany(User::class, '??', '??' , '??'); // what should I insert in '??'
    }

in User Model :

    public function teams()
    {
        return $this->belongsToMany(Team::class, '??', '??' , '??'); // what should I insert in '??'
    }

And also how to create Pivot table without using any convention:

pivot table that I created so far:

class CreateUsersTeamTable extends Migration
{
    protected $collection = "team_user_pivot";
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(  $this->collection
            , function (Blueprint $table) {
            $table->id();
            $table->string('??');  // please help me to fill this field
            $table->string('??');  // please help me to fill this field
            $table->timestamps();
        });
    }

There are '??' in User Model and Team model and Pivot table . help me to fill up each of them

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2025-02-17 10:33:25

它并不像填写字段那样简单,因为您正在为外键使用字符串以及不限制标准的Namig公约。

create_team_user_pivot

protected $collection = "team_user_pivot";

public function up()
{
  Schema::create($this->collection, function (Blueprint $table) {
    $table->id();
    $table->string('team__id')->references('_id')->on('teams');
    $table->string('user__id')->references('_id')->on('users');
    $table->timestamps();
  });
}

您还需要编辑模型:

用户

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    public $table = 'table_users';
    public $incrementing = false;
    public $keyType = 'string';
    protected $primaryKey = '_id';

    protected $fillable = [
        "_id",
        "username",
        "password"
    ];

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

team

class Team extends Model
{
    use HasFactory;

    public $table = 'table_teams';
    public $incrementing = false;
    public $keyType = 'string';
    protected $primaryKey = '_id';

    protected $guarded = [];

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

It's not quite as simple as filling in the fields, as you're using a string for the foreign key as well as not follwing the standard namig conventions.

create_team_user_pivot

protected $collection = "team_user_pivot";

public function up()
{
  Schema::create($this->collection, function (Blueprint $table) {
    $table->id();
    $table->string('team__id')->references('_id')->on('teams');
    $table->string('user__id')->references('_id')->on('users');
    $table->timestamps();
  });
}

You also need to edit the models:

User

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    public $table = 'table_users';
    public $incrementing = false;
    public $keyType = 'string';
    protected $primaryKey = '_id';

    protected $fillable = [
        "_id",
        "username",
        "password"
    ];

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

Team

class Team extends Model
{
    use HasFactory;

    public $table = 'table_teams';
    public $incrementing = false;
    public $keyType = 'string';
    protected $primaryKey = '_id';

    protected $guarded = [];

    public function users()
    {
        return $this->belongsToMany(User::class, 'team_user_pivot', 'team__id', 'user__id');
    }
}
得不到的就毁灭 2025-02-17 10:33:25

您必须

class UserTeam extends Model
{
public $timestamps = false;
protected $table = 'user_has_team';
protected $fillable = ['user_id', 'team_id'];
}

团队模型中创建另一个具有迁移的模型

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

在用户模型中的

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

you have to create another model with migration

class UserTeam extends Model
{
public $timestamps = false;
protected $table = 'user_has_team';
protected $fillable = ['user_id', 'team_id'];
}

in your team model

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

in your user model

public function teams()
{
    return $this->belongsToMany(Team::class, 'user_has_team', 'team_id', 'user_id');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文