Msoft's avatar
Level 1

how to grop cuplicate raw data and print only one data row values in laravel 5.6

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?

0 likes
4 replies
Tray2's avatar

You can use the

->first();

To get only the first record with image into another variable which you display without any foreach.

Msoft's avatar
Level 1

@Tray2 I need foreach because in my table here are more images to print with different vehicle_ids

Cronix's avatar

You shouldn't start a 2nd thread on this identical topic. I went through a lot of time and work helping you with this identical problem yesterday, and you didn't even say if it worked or what was wrong with my last solution. Please stick to your original thread: https://laracasts.com/discuss/channels/laravel/how-to-display-the-last-record-from-a-manytomany-relationship-in-laravel-56?page=2#reply-440636

I'm pretty sure my last post will solve it for you.

Msoft's avatar
Level 1

@Cronix thanks a lot for your help In the previous thread still I am unable to post. that means I did not show post your reply buttons. that the reason didn't say anything about that and start new thread. any way I got success with this blade.

@extends('layouts.app')

@section('content')
<div class="container"> 
    <div class="row"> 
        <div class="col-md-10 col-md-offset-1">



 
<table>
        <tbody>

            @forelse( $vehicles as $vehicule )
           
                <td>
                    @if( $vehicule->uploads->count() > 0 )
                        <a href="{{ route('vechicles.show', $vehicule->id) }}">
                            @php
                                $upload = $vehicule->uploads->sortByDesc('id')->first();
                            @endphp
                            <img src="/images/{{ $upload->resized_name }}"></a>
                        </a>    
                    @else
                        This vehicule does not have any images attached.
                    @endif
                </td>
            @empty
                <td>No vehicules to display.</td>
            @endforelse

        </tbody>

    </table>
    

  



 
</div>

</div>
</div>
@endsection

Please or to participate in this conversation.