我有一个数据库,每篇文章有很多图片,我想为每篇文章获取一张图片,并且只出现一篇文章的图片

问题描述

我正在尝试制作汽车网站的目录。

但是我对照片有问题。我希望在目录中每张卡片都有汽车的图像(汽车有/将有多个图像,但我希望目录中只出现一个),或者如果该汽车没有任何图像,则为预定义的图像在数据库中,但我无法获取

问题是,使用我当前的代码(我在数据库中有 5 辆车,但只有两辆车有照片)只有一辆车带有照片,而其他人即使数据库中有照片也带有照片认图像,我不知道如何更改。

这两个是我的表:

coches(汽车)

| id_coche | id_marca | ... |
| :------- | :------: | :-: |
| 00001    | IS 200 d | ... |
| 00002    | CC 2.O TDI 140 DSG | ... |
| 00003    | S60 1.6D 115 | ... |
| 00004    | Scirocco | ... |
| 00005    | 407 SW 1.6 HSI | ... |

imagenes(图像)

| id_imagen | nombre | id_coche |
| :------- | :------: | :-: |
| 00001    | 1595499950-VW-1 (Small) | 00004    |
| 00002    | 1595499973-VW-2 (Small) | 00004    |
| 00003    | 1595499973-VW-3 (Small) | 00004    |
| 00004    | 1595499973-VW-4 (Small) | 00004    |
| 00005    | 1595499973-VW-5 (Small) | 00004    |
| 00006    | 1608314916-407-1 (Small) | 00005    |
| 00007    | 1608314967-407-9 (Small) | 00005    |
| 00008    | 1608314967-407-10 (Small) | 00005    |
| 00009    | 1608314967-407-5 (Small) | 00005    |
| 00010    | 1608314967-407-7 (Small) | 00005    |

这是我的代码

模型

vehiculos.PHP

public static function getVehiculos() {   
        $vehiculo = Vehiculo::join('marcas','coches.id_marca','=','marcas.id_marca')
        ->join('carrocerias','coches.id_carroceria','carrocerias.id_carroceria')
        ->select('coches.*','marcas.marca as nombre_marca','carrocerias.carroceria as nombre_carroceria')
        ->orderBy('vendido','ASC')
        ->get();
        return $vehiculo;
    } 
*The first two joins are for other tables with which I have no problems.

imagenes.PHP

public static function getimagenesCatalogo() {   
        $imagenes = Imagenes::join('coches','imagenes.id_coche','coches.id_coche')
        ->select('imagenes.*')
        ->take(1)
        ->get();
        return $imagenes;
    }

控制器

车辆控制器.PHP

public function viewVehiculos(){
    $Vehiculo = Vehiculo::getVehiculos();
    $Imagenes = Imagenes::getimagenesCatalogo();

    return view('catalogo')->with('Vehiculo',$Vehiculo)->with('Imagenes',$Imagenes);
}

刀刃

catalogo.blade.PHP

<div class="container">
    <div class="row">
        @foreach ($Vehiculo as $obj)
            <div class="col-md-4 col-sm-6 mt-4">
                <div class="product-grid">
                    <div class="product-image">
                        <a href="{{ route('VerVehiculo',['id'=>$obj->id_coche])}}" class="image">
                            @foreach ($Imagenes as $Imagen)
                                @if ($Imagen->id_coche == $obj->id_coche)
                                    <img src='../img/Coches/{{$Imagen->nombre}}.JPG'/>
                                    @else 
                                        <img src='../img/Coches/sinfoto.jpg'/>
                                @endif
                            @endforeach
                        </a>
                        <?PHP if($obj->vendido == 1){ echo "<span class='product-sale-label'>vendido!</span>";}?>
                        <div class="product-info">
                            <a class="caracteristicas-coche">{{$obj->potencia}} cv | {{$obj->cilindrada}} cc | {{$obj->km}} km </a>
                            <a class="mas-info" href="{{ route('VerVehiculo',['id'=>$obj->id_coche])}}"> MAS INFORMACIÓN </a>
                        </div>
                    </div>
                    <div class="product-content">
                        <h3 class="title"><a href="{{ route('VerVehiculo',['id'=>$obj->id_coche])}}">{{$obj->nombre_marca}} {{$obj->modelo}}</a></h3>
                        <div class="price text-success">{{$obj->precio}}€</div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
</div>

由于我没有足够的声望点,我给你留了一个 imgur链接,并附上几张图片以便更好地理解它:https://imgur.com/a/EZiB8LS

第一张图片是目录,只有最后一张图片和它的照片一起出现,其他的即使他们在数据库中有照片(标致407 SW,如下图所示)出来使用图片

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)