作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个数组。
$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/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!