Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php合并两个不同查询的结果并删除重复项_Php_Mysql_Laravel_Foreach - Fatal编程技术网

Php合并两个不同查询的结果并删除重复项

Php合并两个不同查询的结果并删除重复项,php,mysql,laravel,foreach,Php,Mysql,Laravel,Foreach,我有两个mysql表。第一个表主题图片包含给定主题的默认图片。第二个表custom_theme_images包含用户上载的自定义图像,以覆盖themes表中给定的默认图像 这两个表使用自定义主题图像中的它们\u图像\u id字段相互链接。此外,每个用户都由一个唯一的用户\u业务\u id标识,因为不同的用户如果使用相同的主题,则可以在自己的配置文件中覆盖相同的图像。结构如下: 我想要的是能够使用默认图像显示主题图像表中的所有记录,但是如果给定用户已覆盖自定义主题图像表中的图像,则应显示自定义

我有两个mysql表。第一个表主题图片包含给定主题的默认图片。第二个表custom_theme_images包含用户上载的自定义图像,以覆盖themes表中给定的默认图像

这两个表使用自定义主题图像中的它们\u图像\u id字段相互链接。此外,每个用户都由一个唯一的用户\u业务\u id标识,因为不同的用户如果使用相同的主题,则可以在自己的配置文件中覆盖相同的图像。结构如下:

我想要的是能够使用默认图像显示主题图像表中的所有记录,但是如果给定用户已覆盖自定义主题图像表中的图像,则应显示自定义图像,而不是默认图像

我尝试过的

我已经尝试过内部、左侧和右侧联接,但是它们只返回两个表中匹配的字段,但是如果用户没有覆盖图像,我还需要默认值

然后我决定从两个表中检索两个不同的结果,并尝试合并它们并删除重复项。一个结果包含给定配置文件和主题的所有自定义图像,然后第二个结果包含所有默认值。 代码是拉威尔风格的

代码

<tbody>
    @php
        $tally=1;
    @endphp
    @foreach($custom_theme_image as $custom_image)
        <tr>
            <td>{{$custom_image->theme_image_id}}</td>
            <td>{{$custom_image->theme_image->description ?? 'n/a'}}</td>
            <td>
                <img src="storage/{{$custom_image->image}}" class="w-100 " style="max-width: 75px;"/>
            </td>
            <td> <a href="/theme-images/{{$custom_image->theme_image_id}}/create"><span class="badge bg-primary mr-2">Update</span></a></td>
        </tr>
    @endforeach
    @foreach($theme_images as $theme_image)
        <tr>
            <td>{{$theme_image->id}}</td>
            <td>{{$theme_image->description ?? 'n/a'}}</td>
            <td>
                <img src="{{asset('previewThemes')}}/{{$theme_image->theme->name}}/{{$theme_image->image}}" class="w-100 " style="max-width: 75px;"/>
            </td>
            <td> <a href="/theme-images/{{$theme_image->id}}/create"><span class="badge bg-primary mr-2">Change</span></a></td>
        </tr>
        @php
            $tally=$tally+1;
        @endphp
    @endforeach
</tbody>

@php
$tally=1;
@endphp
@foreach($custom\u theme\u image作为$custom\u image)
{{$custom\u image->theme\u image\u id}
{{$custom\u image->theme\u image->description''n/a'}
image}“class=“w-100”style=“最大宽度:75px;"/>
@endforeach
@foreach($theme\u图像作为$theme\u图像)
{{$theme_image->id}
{{$theme_图像->描述???'n/a'}
theme->name}}/{{$theme\u image->image}}”class=“w-100”style=“max width:75px;”/>
@php
$tally=$tally+1;
@endphp
@endforeach
这两个查询的结果中肯定有重复项,即

两个foreach结果:顶部结果是自定义图像结果,底部是默认图像结果。 现在我想要的是能够合并这些副本以显示自定义图像,但仍然保留其他默认图像记录。。。 最终ID应为3,4,5,6,7。。。3,4,5显示自定义图像,6,7显示默认图像

在这种情况下:

user\u business\u id=2

theme_id=2

假设我已经理解了您的问题,有一个简单得多的解决方案

现在,让我们假设您已经运行了两个查询,您有两个变量,
$themeImages
$themeimagesoverrides
。因为您使用的是Laravel,所以这两个都是集合。您希望获取覆盖集合并按图像id为其设置关键帧,如下所示

$themeImageOverrides=$themeImageOverrides->keyBy('theme\u image\u id');
现在,当您在
$themeImages
中循环时,可以调用
$themeimagesoverrides->get($themeImage->id)
。现在,这个
get()
方法实际上接受了第二个参数,这是在找不到键的值时使用的默认值,因此您可以传入
$themeImage
。这意味着您将获得覆盖或原始图像。由于这两个属性都具有
图像
属性,因此您只需执行以下操作即可确保安全:

$themeImageOverrides->get($themeImage->id,$themeImage)->图像;

希望能有所帮助。

非常感谢您对收藏的深入了解。我读过所有关于拉威尔的收藏,现在我非常开明。我还实施了您的解决方案,它非常棒。唯一的问题是,当我调用$themeImageOverrides->get($themeImage->id,$themeImage->image);它返回该位置的整个数组。然后我如何进入数组中以获得该位置的图像?我很高兴我能帮助@Lefty。啊,你误解了。您没有将
$themeImage->image
作为第二个参数传递,而是传递
$themeImage
并访问结果的
image
属性。如果存在覆盖图像行,则随您调用的
get()
将为您提供覆盖图像行,如果不存在覆盖图像行,则将为您提供默认图像行。完全按照我写的那样使用代码,你会没事的。