Laravel print array in blade php
Asked Answered
V

6

27

I want to show an array in my .blade.php, but it does not work properly so my controller looks like this:

class WatchController extends Controller
{

    public function index()
    {
        $watchFolderPath = 'C:\\xampp\\htdocs\\Pro\\rec\\';
        $watchFolder     = $this->dirToArray($watchFolderPath);
        return view('watch.new')->with('watchFolder', $watchFolder);
    }

    # Get Directories of Path as Array
    function dirToArray($dir) {

        $result = array();

        $cdir = scandir($dir);

        foreach ($cdir as $key => $value)
        {
            if (!in_array($value,array(".","..")))
            {
                if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
                {
                    $result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
                }
                else
                {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }
}

And inside my blade I just tried to call it like this:

{{ $watchFolder }}

but it did not work, I get the following error:

htmlentities() expects parameter 1 to be string, array given

Edit: The array I get shows all Folders/Files with subfolder in a directory. (used dd())

Currently it looks like this:

array:6 [▼
  123123 => array:2 [▼
    "subfolder1" => array:1 [▼
      0 => "video.mpg"
    ]
    "subfolder2" => array:1 [▶]
  ]
  789 => array:2 [▶]
  "folder1" => array:2 [▶]
  "folder2" => array:2 [▶]
  "folder3" => array:2 [▶]
  "folder1" => []
]
Voluble answered 16/3, 2016 at 7:42 Comment(9)
you need to loop over the items, like @foreach just read the blade manualEardrop
Either {{ print_r($array, true) }} or {!! dd($array) !!} will help you :)Selena
I want to show it in my blade beautifully later on..Voluble
So then why haven't you accepted Alexey's answer?Selena
are you even reading the comments?Voluble
I've answered, with my answer and Alexey's answer you have everything you need - if you still can't work it out then that's tough; we've spoon fed you enough.Selena
still no answer to my comment.Voluble
What comment? there are lot's none of which ask a specific question - why not improve your question to be 100% clear as to what you want to achieve, what you have done so far and what you're not able to do...Selena
As a future advice I would suggest you to read the comments to have a better understanding.Voluble
T
35

To just output the array in blade for debugging purposes:

        @php
            var_dump($arr);
        @endphp

Then use a foreach loop as mentioned.

Tiffie answered 16/2, 2018 at 23:47 Comment(0)
S
13

From your question it appears as if you want to output the array for debugging purposes which as I have commented you can simply use <?php and ?> tags within your blade templates.

<?php dd($array); ?>

However, blade has a raw output tag pair which is {!! and !!}.

Once you're ready to display the output of your directory structure you should assign it to your view.

There are 2 quick ways of using it either with Reflection; inside your controller:

class WatchController extends Controller
{
    public function index(\Illuminate\Filesystem\Filesystem $filesystem)
    {
        $files = $filesystem->allFiles($watchFolderPath);

        // Your code.

        return view('name', ['files' => $files]);
    }
}

Or to call it from the container:

class WatchController extends Controller
{
    public function index()
    {
        $files = app('files')->allFiles($watchFolderPath);

        // Your code.

        return view('name', ['files' => $files]);
    }
}

Also you should be using the Filesystem, there is no need to reinvent the wheel – You should read the manual.

Selena answered 16/3, 2016 at 9:10 Comment(0)
U
8

You should use @foreach:

@foreach ($watchFolder as $key => $value)
    Key: {{ $key }}    
    Value: {{ $value }} 
@endforeach

Or

@foreach ($watchFolder as $w)
    {{ $w->name }}    
@endforeach
Unni answered 16/3, 2016 at 7:43 Comment(5)
how would you print out all values of the array?Voluble
If you're asking how to print out all values for debugging, you could try to use dd($watchFolder); in your controller or {{ dd($watchFolder); }} in your view. If you're asking how to print out array values in profuction, look at the code in my answer.Unni
does not work I get following error:ErrorException in helpers.php line 469: htmlentities() expects parameter 1 to be string, array givenVoluble
That's because you're building complicated array, please post your array structure example in your question, so we could be able to help you.Unni
In blade {{ and }} are replacements for <?php echo and ?> and are also parsed through e() for escaping bad things. Therefore it would need the literal tags for HTML output or RAW php; {!! and !!} or <?php and ?> are also free to use inside a blade template (although frowned upon).Selena
F
5

For those that might come looking...like me

echo implode("\n",$array);

else if its multidimensional array

echo implode("\n",array_collapse($array));
Fitting answered 27/5, 2018 at 18:51 Comment(0)
O
3

To output your $watchFolder array in a blade file, you can use the following method:

<pre><code>{{ json_encode($watchFolder, JSON_PRETTY_PRINT) }}</code></pre>

This will display a nicely formatted JSON version of your array and make it more readable:

{
    "123123": {
        "subfolder1": [
            "video.mpg"
        ],
        "subfolder2": []
    },
    "789": [],
    "folder1": [],
    "folder2": [],
    "folder3": []
}

You can also remove the JSON_PRETTY_PRINT if you only need to display it in a single line:

{"123123":{"subfolder1":["video.mpg"],"subfolder2":[]},"789":[],"folder1":[],"folder2":[],"folder3":[]}
Ohg answered 4/12, 2021 at 1:44 Comment(0)
K
3

You may simply use :

@dump($arr)
Kammerer answered 23/1, 2022 at 15:48 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Kelvinkelwen

© 2022 - 2024 — McMap. All rights reserved.