You can use the
->first();
To get only the first record with image into another variable which you display without any foreach.
Hi, I am working on laravel 5.6 and I have multiple images in my uploads table with related to vehicle_id, as foreign key like this,
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 hasMany relationship with the uploads table,
public function uploads()
{
return $this->hasMany(Upload::class);
}
VehicleController is loaded with eager loader,
public function index()
{
$vehicles = Vehicle::with('uploads')->get();
return view('vechicles.index')->withVehicles($vehicles);
}
and My blade file is
@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. 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? I think group duplicate vehicle_id images and print only descending Oder first image?
Please or to participate in this conversation.