【问题标题】:How to display the last record from a manyToMany relationship in Laravel 5.6如何在 Laravel 5.6 中显示多对多关系的最后一条记录
【发布时间】:2018-07-13 15:40:15
【问题描述】:

我的上传表中有多个与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

VehicleController中使用Eager loader的车辆表与图像表和数据图有多对多的关系,

$vehicles = Vehicle::with('uploads')->get();
        return view('vechicles.index')->withVehicles($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

我的问题现在出现了,通过这种方式,我可以在表格中显示与正确 vehicle_id 相关的所有图像,但是,我只需要显示一张图像(匹配 vehicle_id) 就像上面那行的缩略图一样。那我该如何配置呢?实际上,我只需要在 index.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

【问题讨论】:

  • 请发布uploads关系。
  • 请查看我更新的用户模型

标签: php laravel laravel-5.6


【解决方案1】:

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

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);
        }
    }
?>

资源/视图/车辆/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>

这里我们使用 Laravel Collection -&gt;first() 方法来获取第一个方法,但是不要忘记(在示例中这样做)在获取时检查是否有任何上传(或任何关系)第一个元素。

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

【讨论】:

  • 发生此错误试图获取非对象的属性(查看:C:\Users\Banda\Desktop\acxian\resources\views\vechicles\index.blade.php)
  • 检查您在上传表中的字段是否与我在此示例中编写的属性匹配:fileName, resized_name
【解决方案2】:

请尝试latest方法获取最新型号。

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

【讨论】:

  • 你的public函数thumbnail()是什么意思thumbnail是模型吗?
  • 请检查您上传的内容
  • 据我所知应该是模特吧?
  • thumbnail 方法将返回最后插入的Upload 模型
猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 2017-08-20
  • 1970-01-01
  • 2011-01-07
  • 2017-06-02
相关资源
最近更新 更多