dimanche 15 juillet 2018

how to print only one data record from duplicate foreign ids in the laravel 5.6

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 thumbnail() {
    return $this->hasMany(Upload::class)->orderByDesc('id');
}

VehicleController is,

public function index()
    {
        $vehicles = Vehicle::with('thumbnail')->get();
        return view('vechicles.index')->withVehicles($vehicles);
}

and data showing index.blade.php file is,

@if($vehicles)
@foreach($vehicles as $vehicle)





<hr>

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

                </tr>


@endforeach
@endif

but still in my view file I can show multiple images which related to vehicle_id (not only show only one image regarding to vehicle_id), then, how can I show only one image to relevant each vehicle_id?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire