如何显示 Laravel 5.6 中多对多关系的最后一条记录

How to display the last record from a manyToMany relationship in Laravel 5.6

我上传的多张图片 table 与 vehicle_id 相关,像这样的外键,

uploads table
id          fileName        vehicle_id
1            1.jpg               1
2            2.jpg               1
3            3.jpg               1
4            4.jpg               1
5            28.png              2
6            28.png              2
7            29.png              2
8            30.png              3
9            31.png              3
10           56.png              3

车辆 table 与图像 table 和在 VehicleController 中使用 eager loader 的数据抓取有多对多的关系,

$vehicles = Vehicle::with('uploads')->get();
        return view('vechicles.index')->withVehicles($vehicles);

现在这些图像显示在 vehicles/index.blade.php 文件中

 @foreach($vehicle->uploads as $upload)

         <tr>
                        <td><a href="{{route('vechicles.show',$vehicle->id)}}"><img src="/images/{{ $upload->resized_name }}"></a></td>

                    </tr>

        @endforeach

我的问题现在出现了,这样,我可以显示 table 中与适当 vehicle_id 相关的所有图像,但是,我需要仅显示一张图片(匹配 vehicle_id),如缩略图到上一行。那我该如何配置呢?实际上我只需要在索引中打印一张图像。blade.php 到每个 vehicle_id 中的相关图像, 作为示例,这里有 4 张与 vehicle_id 1 相关的图像。但我只需要打印降序或一张图像即可打印,并且与 vehicle_id 2 等相同......如何做到这一点?

已更新用户模型

class Upload extends Model
{
    public function vehicle()
    {
        return $this->belongsTo(Vehicle::class);
    }
}

更新车辆模型

 class Vehicle {
    public function thumbnail() {
        return $this->hasOne(Upload::class)->orderByDesc('id');
    }
}

$vehicles = Vehicle::with('thumbnail')->get();

当前 Blade 文件

 @if($vehicles)
@foreach($vehicles as $vehicle)
{{$vehicle->district}}
{{$vehicle->town}}
{{$vehicle->brand}}
{{$vehicle->model}}

<hr>

 <tr>
  @foreach($vehicle->thumbnail as $aaa)
                    <img src="/images/{{ $aaa->resized_name }}">
                    @endforeach

                </tr>


@endforeach
@endif

@Jonas Staudenmeier 解决方案是最干净的。但是,如果您出于任何原因无法更新模型,您可以在任何 blade 模板中使用它。

app/Http/Controllers/VehiculeController.php

<?php
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Vehicule;

    class VehiculeController extends Controller {
        public function index() {
            $vehicules = Vehicule::with('uploads')->get();

            return view('vehicule.index')->withVehicules($vehicules);
        }
    }
?>

resources/views/vehicule/index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <table>
        <tbody>
            @forelse( $vehicules as $vehicule )
                <td>
                    @if( $vehicule->uploads->count() > 0 )
                        <a href="{{ route('vehicules.show', $vehicule->id) }}">
                            @php
                                $upload = $vehicule->uploads->sortByDesc('id')->first();
                            @endphp
                            <img src="{{ $upload->resized_name }}" alt="{{ $upload->fileName }}" />
                        </a>    
                    @else
                        This vehicule does not have any images attached.
                    @endif
                </td>
            @empty
                <td>No vehicules to display.</td>
            @endforelse
        </tbody>
    </table>
</body>
</html>

这里我们使用LaravelCollection->first()方法获取第一个方法,但是不要忘记(示例中已经这样做)检查是否有上传(或任何一般方式的关系)当你得到第一个元素时。

当您获取模型的关系时,所有 Laravel Collection methods 都可用,直到您使用修整器(firstall、...)。

请尝试关系中的latest方法获取最新型号。

class Vehicle {
    public function thumbnail() {
        return $this->hasOne(Upload::class)->latest();
    }
}