[英]How to display the last record from a manyToMany relationship in Laravel 5.6
I have multiple images in my uploads table with related to vehicle_id, as foreign key like this, 我的上传表中有多张图片,其中包含与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
The vehicle table have many to many relationship with the image table and data grap using eager loader in VehicleController, 车辆表格与图像表格和使用VehicleController中的eager loader进行的数据抓取有很多关系,
$vehicles = Vehicle::with('uploads')->get();
return view('vechicles.index')->withVehicles($vehicles);
Now these images are showing in the vehicles/index.blade.php file 现在这些图像显示在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
My problem is occurred now, in this way, I can show all images in the table whith related to proper vehicle_id but, I need only show one image (to matching vehicle_id ) like thumbnail to above line. 我的问题现在发生了,用这种方式,我可以在表中显示与正确的vehicle_id相关的所有图像,但是,我只需要在上面一行显示一个图像(与匹配的vehicle_id相匹配),如缩略图。 Then how can I configure this? 那我该如何配置呢? actually I need only one image to print in index.blade.php to related in each vehicle_id, as an example here are 4 images with related to vehicle_id 1. but I need only print decending oder one image to print and same to vehicle_id 2 and etc.... how can do this? 实际上,我只需要在index.blade.php中打印一张要在每个vehicle_id中关联的图像,例如,这里有4张与vehicle_id 1相关的图像,但是我只需要打印下降的一张图像就可以打印,并且与vehicle_id 2相同,并且等等...该怎么做?
Updated User Model 更新的用户模型
class Upload extends Model
{
public function vehicle()
{
return $this->belongsTo(Vehicle::class);
}
}
updated Vehicle Model 更新的车辆型号
class Vehicle {
public function thumbnail() {
return $this->hasOne(Upload::class)->orderByDesc('id');
}
}
$vehicles = Vehicle::with('thumbnail')->get();
current Blade file 当前的刀片文件
@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 solution is the cleanest. @Jonas Staudenmeier解决方案是最干净的。 However, if you cannot for any reason update the model, you can use this in any of your blade template. 但是,如果由于某种原因无法更新模型,则可以在任何刀片服务器模板中使用它。
app/Http/Controllers/VehiculeController.php 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 资源/视图/车辆/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>
Here we use Laravel Collection ->first()
method to get the first method, but do not forget (this is done in the example) to check if there is any uploads (or any relationship in a general manner) when you get the first element. 在这里,我们使用Laravel Collection- ->first()
方法获取第一个方法,但是不要忘记(在示例中已完成)在获取第一个方法时检查是否存在任何上传(或以一般方式存在任何关系)元件。
When you fetch relations of a model, all Laravel Collection methods are available until you use a finisher ( first
, all
, ...). 当您获取模型的关系时,所有的Laravel Collection方法都可用,直到您使用了修整器( first
, all
,...)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.