tighten / collect

A Collections-only split from Laravel's Illuminate Support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No High Order Messages on Collections?

godbout opened this issue · comments

commented

As usual, I might be missing something here so please advise me if I'm wrong.

I'm trying to generate a PHP file with Jigsaw (for serving random writings).

I have a part of the following (ugly) code that fills an array with the URLs of my writings:

$writings->each(function ($writing) {
    echo "\r\n    '{$writing->getUrl()}',";
});

this generates:

    '/writings/3',
    '/writings/again',

I'm expected to get the same as above by using high order message like this:

echo "\r\n    '{$writings->each->getUrl()}',";

but instead the output is this:

'{"3":{"production":false,"baseUrl":"","collections":{"writings":{"extends":"_layouts.master","section":"body"}},"section":"body","extends":"_layouts.master","title":3,"kind":"article","type":"conscious","created_at":"2016-11-18 01:01","_meta":{"filename":"3","baseUrl":"","extension":"md","collection":"writings","collectionName":"writings","source":"\/Users\/guill\/Development\/sleeplessmind.info\/source\/_writings","path":["\/writings\/3"],"url":["\/writings\/3"],"previousItem":null,"nextItem":"again"}},"again":{"production":false,"baseUrl":"","collections":{"writings":{"extends":"_layouts.master","section":"body"}},"section":"body","extends":"_layouts.master","title":"Again","kind":"article","type":"unconscious","created_at":"2017-05-20 03:26","_meta":{"filename":"again","baseUrl":"","extension":"md","collection":"writings","collectionName":"writings","source":"\/Users\/guill\/Development\/sleeplessmind.info\/source\/_writings","path":["\/writings\/again"],"url":["\/writings\/again"],"previousItem":"3","nextItem":null}}}',

The each doesn't seem to loop over each writing, and rather than getting the result of getUrl() for each writing it seems that I'm getting the Collection object.

It's not a big issue as I can get the ouput I need. But I'm just curious about where I might be making a mistake. I like clean code :))

Thanks!

@godbout My guess is that this isn't about collect but instead that you put that each in a context where you expect it to echo but instead you sent the collection object itself to the view.

Can you spin up a plain PHP project, include collect, and reproduce this issue?

commented

Hey @mattstauffer, thanks for the quick answer. I'm realizing that I've probably posted this issue in the wrong repo, posting it in tightenco/jigsaw might have been better instead. What I thought was that maybe, like you said, Jigsaw was actually not handling or sending the collection correctly? (Which I doubt, it's probably me who is not using it correctly.)

I'll try a plain PHP project and will let you know. If it's fine then I'll write something in Jigsaw.

commented

Ok I've tried with plain PHP and I've had the same issue. But it's on me. I realize now that it doesn't make sense to call echo on the result of each, I misunderstood how collect works. If I add a method that actually does the echo and calls it from the each then it works fine. Thanks for the help!