Skip to content
Advertisement

how to pass the variable to the input?

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!!

Advertisement

Answer

@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) }}">
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement