Laravel关系 - 将数组键设置为从列的名称

发布于 2025-01-22 15:38:13 字数 7077 浏览 0 评论 0原文

对于我的英语不好,我提前表示歉意。

我的模型:文章,图像和图像儿童的

文章:

class Article extends Model
{

    public function image()
    {
        return $this->hasOne(Image::class, 'id');
    }

}

图像:

class Image extends Model
{

    public function images()
    {
        return $this->hasMany(ImageChild::class);
    }

}

ImageChild:

class ImageChild extends Model
{

    public function image()
    {
        return $this->belongsTo(Image::class);
    }

}

使用:

$articles = Article::with('image.images')->orderBy('id', 'desc')->take(6)->get()->toArray();

return dd($articles);

返回:

5 => array:9 [▼
    "id" => 2
    "title" => "Lorem ipsum"
    "slug" => "Lorem ipsum"
    "content" => "Lorem ipsum"
    "user_id" => 1
    "image_id" => 3
    "created_at" => "2022-04-19T10:04:12.000000Z"
    "updated_at" => "2022-04-19T10:04:12.000000Z"
    "image" => array:5 [▼
      "id" => 2
      "alt" => "Lorem ipsum"
      "created_at" => "2022-04-19 06:30:41"
      "updated_at" => "2022-04-19 06:30:41"
      "sizes" => array:7 [▼
        0 => array:8 [▼
          "id" => 8
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_large.png"
          "size" => "large"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        1 => array:8 [▼
          "id" => 9
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_banner.png"
          "size" => "banner"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        2 => array:8 [▼
          "id" => 10
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_slide.png"
          "size" => "slide"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        3 => array:8 [▼
          "id" => 11
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_cover.png"
          "size" => "cover"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        4 => array:8 [▼
          "id" => 12
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_thumbnail.png"
          "size" => "thumbnail"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        5 => array:8 [▼
          "id" => 13
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_small.png"
          "size" => "small"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        6 => array:8 [▼
          "id" => 14
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_mobile.png"
          "size" => "mobile"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
      ]
    ]
  ]

我的目标是设置数组键(主数组 - > image - > size)所有孩子的所有孩子都来自[0,1,1 ,2,3 ...]大小的名称,例如:

...
"sizes" => array:7 [▼
        "large" => array:8 [▼
          "id" => 8
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_large.png"
          "size" => "large"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "banner" => array:8 [▼
          "id" => 9
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_banner.png"
          "size" => "banner"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "slide" => array:8 [▼
          "id" => 10
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_slide.png"
          "size" => "slide"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "cover" => array:8 [▼
          "id" => 11
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_cover.png"
          "size" => "cover"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "thumbnail" => array:8 [▼
          "id" => 12
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_thumbnail.png"
          "size" => "thumbnail"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "small" => array:8 [▼
          "id" => 13
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_small.png"
          "size" => "small"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "mobile" => array:8 [▼
          "id" => 14
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_mobile.png"
          "size" => "mobile"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
      ]
...

我尝试使用函数keyby(),但是我没有达到所需的效果

,希望我很清楚地描述了

如何实现这一目标?

非常感谢您的每个答案

I apologize in advance for my not good English.

My model's: Article, Image and Image child's

Article:

class Article extends Model
{

    public function image()
    {
        return $this->hasOne(Image::class, 'id');
    }

}

Image:

class Image extends Model
{

    public function images()
    {
        return $this->hasMany(ImageChild::class);
    }

}

ImageChild:

class ImageChild extends Model
{

    public function image()
    {
        return $this->belongsTo(Image::class);
    }

}

After using:

$articles = Article::with('image.images')->orderBy('id', 'desc')->take(6)->get()->toArray();

return dd($articles);

return for example:

5 => array:9 [▼
    "id" => 2
    "title" => "Lorem ipsum"
    "slug" => "Lorem ipsum"
    "content" => "Lorem ipsum"
    "user_id" => 1
    "image_id" => 3
    "created_at" => "2022-04-19T10:04:12.000000Z"
    "updated_at" => "2022-04-19T10:04:12.000000Z"
    "image" => array:5 [▼
      "id" => 2
      "alt" => "Lorem ipsum"
      "created_at" => "2022-04-19 06:30:41"
      "updated_at" => "2022-04-19 06:30:41"
      "sizes" => array:7 [▼
        0 => array:8 [▼
          "id" => 8
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_large.png"
          "size" => "large"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        1 => array:8 [▼
          "id" => 9
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_banner.png"
          "size" => "banner"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        2 => array:8 [▼
          "id" => 10
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_slide.png"
          "size" => "slide"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        3 => array:8 [▼
          "id" => 11
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_cover.png"
          "size" => "cover"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        4 => array:8 [▼
          "id" => 12
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_thumbnail.png"
          "size" => "thumbnail"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        5 => array:8 [▼
          "id" => 13
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_small.png"
          "size" => "small"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        6 => array:8 [▼
          "id" => 14
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_mobile.png"
          "size" => "mobile"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
      ]
    ]
  ]

My goal is set array key in (main array -> image -> sizes) all of the child's from [0,1,2,3...] to name of size like:

...
"sizes" => array:7 [▼
        "large" => array:8 [▼
          "id" => 8
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_large.png"
          "size" => "large"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "banner" => array:8 [▼
          "id" => 9
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_banner.png"
          "size" => "banner"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "slide" => array:8 [▼
          "id" => 10
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_slide.png"
          "size" => "slide"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "cover" => array:8 [▼
          "id" => 11
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_cover.png"
          "size" => "cover"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "thumbnail" => array:8 [▼
          "id" => 12
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_thumbnail.png"
          "size" => "thumbnail"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "small" => array:8 [▼
          "id" => 13
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_small.png"
          "size" => "small"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
        "mobile" => array:8 [▼
          "id" => 14
          "name" => "s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd"
          "ext" => "png"
          "src" => "/images/upload/2022/2/s8UUjBlvjWjZHBHjFSqeNhJuo4AlLd_mobile.png"
          "size" => "mobile"
          "image_id" => 2
          "created_at" => "2022-04-19 06:30:42"
          "updated_at" => "2022-04-19 06:30:42"
        ]
      ]
...

I tried use function keyBy(), but I did not achieve the desired effect

I hope I have described the situation quite clearly

Any idea how this can be achieved?

Thank you very much in advance for each answer

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

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

发布评论

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

评论(2

残龙傲雪 2025-01-29 15:38:13

我找到了一个解决方案,但我不知道这是否正确。如果有人评论此解决方案,我将很感激。

在模型图像中,我添加了:

    protected $appends = ['sizes'];

    public function getSizesAttribute()
    {
      return $this->images()->get()->keyBy('size');
    }

效果是:

  0 => array:9 [▼
    "id" => 4
    "title" => "Lorem ipsum dolor"
    "slug" => "lorem-ipsum-dolor"
    "content" => "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quibusdam magnam maiores sapiente voluptatem quidem obcaecati iusto consequatur repellat, laborum aliqu ▶"
    "user_id" => 1
    "image_id" => 5
    "created_at" => "2022-04-19T11:30:23.000000Z"
    "updated_at" => "2022-04-19T11:30:23.000000Z"
    "image" => array:5 [▼
      "id" => 4
      "alt" => "Lorem ipsum dolor"
      "created_at" => "2022-04-19 06:31:27"
      "updated_at" => "2022-04-19 06:31:27"
      "sizes" => array:7 [▼
        "large" => array:8 [▼
          "id" => 22
          "name" => "u7osaVoZ5mb2ikEfl9aWWcloE0HOjL"
          "ext" => "jpeg"
          "src" => "/images/upload/2022/4/u7osaVoZ5mb2ikEfl9aWWcloE0HOjL_large.jpeg"
          "size" => "large"
          "image_id" => 4
          "created_at" => "2022-04-19 06:31:31"
          "updated_at" => "2022-04-19 06:31:31"
        ]
        "banner" => array:8 [▶]
        "slide" => array:8 [▶]
        "cover" => array:8 [▶]
        "thumbnail" => array:8 [▶]
        "small" => array:8 [▶]
        "mobile" => array:8 [▶]
      ]
    ]
  ]

您如何看待它?

I found a solution, but I don't know if it's right. I will be grateful if someone comments this solution.

In model Image I added:

    protected $appends = ['sizes'];

    public function getSizesAttribute()
    {
      return $this->images()->get()->keyBy('size');
    }

Effect is:

  0 => array:9 [▼
    "id" => 4
    "title" => "Lorem ipsum dolor"
    "slug" => "lorem-ipsum-dolor"
    "content" => "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quibusdam magnam maiores sapiente voluptatem quidem obcaecati iusto consequatur repellat, laborum aliqu ▶"
    "user_id" => 1
    "image_id" => 5
    "created_at" => "2022-04-19T11:30:23.000000Z"
    "updated_at" => "2022-04-19T11:30:23.000000Z"
    "image" => array:5 [▼
      "id" => 4
      "alt" => "Lorem ipsum dolor"
      "created_at" => "2022-04-19 06:31:27"
      "updated_at" => "2022-04-19 06:31:27"
      "sizes" => array:7 [▼
        "large" => array:8 [▼
          "id" => 22
          "name" => "u7osaVoZ5mb2ikEfl9aWWcloE0HOjL"
          "ext" => "jpeg"
          "src" => "/images/upload/2022/4/u7osaVoZ5mb2ikEfl9aWWcloE0HOjL_large.jpeg"
          "size" => "large"
          "image_id" => 4
          "created_at" => "2022-04-19 06:31:31"
          "updated_at" => "2022-04-19 06:31:31"
        ]
        "banner" => array:8 [▶]
        "slide" => array:8 [▶]
        "cover" => array:8 [▶]
        "thumbnail" => array:8 [▶]
        "small" => array:8 [▶]
        "mobile" => array:8 [▶]
      ]
    ]
  ]

What do you think about it?

感性 2025-01-29 15:38:13

您也可以将 maptogroups 方法用于大小数组。 之前,您应该使用 collect($ yourarray)将该数组转换为集合

但是在此

collect($yourArray)->mapToGroups(function($item) {
        return $item->size= $item;
    });

you can also use mapToGroups method to your sizes array. but before that you should convert that array to collection by using collect($yourArray)

Try like this

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