Php 我有一个数据库,每篇文章都有很多图片,我希望每篇文章都有一个图片,并且只显示一篇文章的图片

Php 我有一个数据库,每篇文章都有很多图片,我希望每篇文章都有一个图片,并且只显示一篇文章的图片,php,database,laravel,if-statement,foreach,Php,Database,Laravel,If Statement,Foreach,我正在尝试制作一个汽车网站的目录 但是我对照片有问题。我希望在目录中,每张卡都有汽车的图像(汽车有/将有多个图像,但我只希望目录中出现一个),或者,如果汽车在数据库中没有任何图像,但我无法获取它,则需要一个预定义的图像 问题是,根据我目前的代码(我在数据库中有5辆车,但只有两辆车有照片),只有一辆车有照片,而其他车即使在数据库中有照片也会显示默认图像,我不知道如何更改 这两张是我的桌子: coches(汽车) 图像(图像) 这是我的代码: 型号 vehiculos.php public stat

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

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

问题是,根据我目前的代码(我在数据库中有5辆车,但只有两辆车有照片),只有一辆车有照片,而其他车即使在数据库中有照片也会显示默认图像,我不知道如何更改

这两张是我的桌子:

coches(汽车)

图像(图像)

这是我的代码:

型号

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;
    }
控制器

VehicleuloController.php

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

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

catalogo.blade.php


@foreach($Vehiculo作为$obj)

嗨,凯特。虽然您的代码可能会在完成一些工作后工作,但为什么不尝试利用laravel关系呢?在您的案例中,汽车与图像的关系似乎是一对多(请参阅)。使用laravel关系有很多优点,并且使用Eloquent可以使查询更加容易。
public static function getImagenesCatalogo() {   
        $imagenes = Imagenes::join('coches', 'imagenes.id_coche', '=', 'coches.id_coche')
        ->select('imagenes.*')
        ->take(1)
        ->get();
        return $imagenes;
    }
public function viewVehiculos(){
    $Vehiculo = Vehiculo::getVehiculos();
    $Imagenes = Imagenes::getImagenesCatalogo();

    return view('catalogo')->with('Vehiculo', $Vehiculo)->with('Imagenes', $Imagenes);
}
<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>