gpt4 book ai didi

php - 拉维尔 |具有保留键的PHP数组递归合并

转载 作者:行者123 更新时间:2023-12-04 04:01:28 25 4
gpt4 key购买 nike

我有三个数组。

$data1 = []; $data2 =[]; $data3 = [];

foreach($request->clients as $client)
{
$data1[]= [$client=>['role'=>'client']];
}

foreach($request->employees as $employee)
{
$data2[]= [$employee=>['role'=>'employee']];
}

foreach($request->users as $user)
{
$data3[] = [$user=>['role'=>'user']];
}

$data1 = [1=>['role'=>'client'], 2=>['role'=>'client']];
$data2 = [1=>['role'=>'employee']];
$data3 = [1=>['role'=>'user']];

//merge or recursive merge or... $data1, $data2, $data3.

$result = [1=>['role'=>'client'], 2=>['role'=>'user'], 1=>['role'=>'user'], 1=>['role'=>'employee']];

我怎样才能得到像上面这样的结果?这是Laravel Many to Many Sync with additional column .

谢谢

最佳答案

在与 OP 讨论后,需要更改结构以允许关系角色能够与许多角色相关联。为了实现这一点,我们可以这样做。

架构:

Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
});

Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
});

Schema::create('memberships', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('team_id');
// Any other data you want here is fine
});

Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
});

Schema::create('membership_role', function (Blueprint $table) {
$table->unsignedBigInteger('membership_id');
$table->unsignedBigInteger('role_id');
});

现在为 Membership.php

class Membership extends Model
{
public static function create(User $user, Team $team, array $roles = [])
{
$membership = new self();
$membership->user_id = $user->id;
$membership->team_id = $team->id;
$membership->save();


$attach = [];

foreach ($roles as $role) {
$attach[] = Role::resolveId($role);
}

$membership->roles()->attach($attach);

return $membership;
}

public function roles()
{
return $this->belongsToMany(Role::class);
}

public function syncRoles(array $roles)
{
$sync = [];

foreach ($roles as $role) {
$sync[] = Role::resolveId($role);
}

$this->roles()->sync($sync);
}
}

和Role.php

class Role extends Model
{
const CLIENT = 'client';
const EMPLOYEE = 'employee';
const USER = 'user';

public function memberships()
{
return $this->belongsToMany(Membership::class);
}

public static function resolveId()
{
if (is_int($role)) {
return $role;
}

if (is_string($role)) {
$role = Role::where('name', $role)->first();
}


return $role->id;
}
}

现在您可以假设其他类的实现具有明显的关系并执行:

foreach($request->clients as $client)
{
if (!isset($roleSync[$client])) {
$roleSync[$client] = [];
}

$roleSync[$client][] = Role::CLIENT;
}

foreach($request->employees as $employee)
{
if (!isset($roleSync[$employee])) {
$roleSync[$employee] = [];
}

$roleSync[$employee][] = Role::EMPLOYEE;
}

foreach($request->users as $user)
{
if (!isset($roleSync[$user])) {
$roleSync[$user] = [];
}

$roleSync[$user][] = Role::USER;
}

$ids = array_keys($roleSync);
$users = User::with('membership.roles')
->whereIn('id', $ids)
->get();

foreach ($users as $user) {
$roles = $roleSync[$user->id];
$user->membership->syncRoles($roles)
}

关于php - 拉维尔 |具有保留键的PHP数组递归合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63009112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com