Ramzan Mahmood
Ramzan Mahmood

Reputation: 1901

Display image thumbnails on blade view (laravel 5.3)

I have to view image thumbnails on my view.blade.php i have directory as assets/students/id id is the directory of every student id it will be 1 for student1 and 2 for stident2 mean directory will become as

assets/students/1

assets/students/2

Every directory have 2 images inside one is with default name and 2nd is thumbnail.png i want to view every thumbnail form each student directory to my view my method is as

$students = Students::orderBy('id', 'desc')->get();

i am getting all paths in method as

$attached = [];

    $destination = config('school.attachment_path.students');
    if(is_dir($destination)) {
        $pix = File::allFiles($destination);
        foreach ($pix as $attachment) {
            $attached[] = pathinfo($attachment);
        }

    }

If i do this

 dd($attached);
            die;

It giving me output as

    array:4 [▼
  0 => array:4 [▼
    "dirname" => "C:\wamp64\www\achool-app\assets/student/\1"
    "basename" => "org.PNG"
    "extension" => "PNG"
    "filename" => "org"
  ]
  1 => array:4 [▼
    "dirname" => "C:\wamp64\www\achool-app\assets/student/\1"
    "basename" => "thumbnail.png"
    "extension" => "png"
    "filename" => "thumbnail"
  ]
  2 => array:4 [▼
    "dirname" => "C:\wamp64\www\achool-app\assets/student/\2"
    "basename" => "Capture.PNG"
    "extension" => "PNG"
    "filename" => "Capture"
  ]
  3 => array:4 [▼
    "dirname" => "C:\wamp64\www\achool-app\assets/student/\2"
    "basename" => "thumbnail.png"
    "extension" => "png"
    "filename" => "thumbnail"
  ]
]

I want to view thumbnail of both directories in my view i try as

return view('student-gallery.view')->with(compact('attached' , 'students'));

View

@foreach($attached_files as $attached_file)
<div">
  <img src="{{URL::to('/assets/students/' . $students->id . '/thumbnail.png')}}" alt="" width="50px" height="50px">
 </div>
@endforeach

Unfortunately its not working its giving error of $students->id as

Undefined property: Illuminate\Database\Eloquent\Collection::$id

And not displaying thumbnails

Thanks if any body can help to fix it

Upvotes: 2

Views: 2597

Answers (2)

vallabh
vallabh

Reputation: 21

Try this.. it will show all students thumbmails

@foreach ($students as $student) 
<div>
<img src="{{ asset('/assets/students/' . $student->id .'/thumbnail.png') }}" alt="" width="50px" height="50px">
</div>
@endforeach

Upvotes: 1

Naincy
Naincy

Reputation: 2943

$students doesn't like be that it having only one record. You need to do for loop for it to fetch data of each student.

@if (! $students->isEmpty()) {  // Also check if students data is coming in collection
     @foreach ($students as $student)
        // echo $student->id;
        @foreach($attached_files as $attached_file)
          <div>
          <img src="{{URL::to('/assets/students/' . $student->id . '/thumbnail.png')}}" alt="" width="50px" height="50px">
          </div>
        @endforeach
     @endforeach
@endif

For better understanding, you can try print_r($students).

Upvotes: 0

Related Questions