Home » Php » php – how to pass the variable to the input?

php – how to pass the variable to the input?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have this code in my blade:

@php
    $images_products = $product->images()->where('main_image', 0)->get();

    foreach ($images_products as $image) {

        $preloadedFiles[] = [
            "name"  => $image->image,
            "type"  => FileUploader::mime_content_type($image->image_path),
            "size"  => filesize('uploads/products/'. $image['image']),
            "file"  => $image->image_path,
            "local" => $image->image_path,
        ];

    }
@endphp

{{-- @dd(json_encode($preloadedFiles)) --}}

<input type="file" name="images" data-fileuploader-files="{{ json_encode($preloadedFiles) }}">

I got the error:

Undefined variable: preloadedFiles

but when I do the dd for same variable it’s work!!

How to&Answers:
@php
    $images_products = $product->images()->where('main_image', 0)->get();

    // Just initialize empty $preloadedFiles array here
    $preloadedFiles = [];

    foreach ($images_products as $image) {

        $preloadedFiles[] = [
            "name"  => $image->image,
            "type"  => FileUploader::mime_content_type($image->image_path),
            "size"  => filesize('uploads/products/'. $image['image']),
            "file"  => $image->image_path,
            "local" => $image->image_path,
        ];

    }
@endphp

{{-- @dd(json_encode($preloadedFiles)) --}}

<input type="file" name="images" data-fileuploader-files="{{ json_encode($preloadedFiles) }}">