简体   繁体   English

在刀片视图上显示图像缩略图(laravel 5.3)

[英]Display image thumbnails on blade view (laravel 5.3)

I have to view image thumbnails on my view.blade.php i have directory as as sets/students/id id is the directory of every student id it will be 1 for st udent1 and 2 for stident2 mean directory will become as 我必须在我的view.blade.php上查看图像缩略图我有目录和sets / students / id id是每个学生ID的目录,对于st udent1 将是1,而对于stident2,2将表示目录将变为as

assets/students/1 资产/生/ 1

assets/students/2 资产/学生/ 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 每个目录中有2个图像,其中一个是默认名称,第二个是thumbnail.png我想查看每个学生目录的每个缩略图到我的视图我的方法是

$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 不幸的是,它没有发挥$ students-> id的错误

Undefined property: Illuminate\\Database\\Eloquent\\Collection::$id 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id

And not displaying thumbnails 而不是显示缩略图

Thanks if any body can help to fix it 谢谢,如果有任何身体可以帮助解决它

$students doesn't like be that it having only one record. $ students不喜欢它只有一个记录。 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). 为了更好地理解,您可以尝试print_r($学生)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM