Laravel关系 - 将数组键设置为从列的名称
对于我的英语不好,我提前表示歉意。
我的模型:文章,图像和图像儿童的
文章:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个解决方案,但我不知道这是否正确。如果有人评论此解决方案,我将很感激。
在模型图像中,我添加了:
效果是:
您如何看待它?
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:
Effect is:
What do you think about it?
您也可以将 maptogroups 方法用于大小数组。 之前,您应该使用 collect($ yourarray)将该数组转换为集合
但是在此
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