conarwelsh / nettuts-laravel4-and-backbone

A tutorial to show one way to combine both Laravel-4 and Backbone.js
138 stars 55 forks source link

Weird Bug related to Eloquent and Mustache at /post/{id} page #4

Closed humuslokiy closed 11 years ago

humuslokiy commented 11 years ago

Hi I'm having trouble with generating html output at laravel server side.

With Javascript Disabled in Google Chrome, there are posts BUT without comments in /posts/{id}.

Here's my route.

Route::get('posts/{id}', function($id)
{
    $post = App::make('PostRepositoryInterface')->findById($id);
    return View::make('layouts.application')->nest('content', 'posts.show', array(
        'post' => $post,
    ));

});

I added debug code in "show.mustache" file to findout what is happening.

<pre>
    {{{post}}}
</pre>

Here is my show.mustache

<article>
    <h3>
        {{ post.title }} {{ post.id }}
        <small>{{ post.author_name }}</small>
    </h3>
    <div>
        {{ post.content }}
    </div>
</article>

<div>

    <pre>
    {{{post}}}
    </pre>

    <h2>Add A Comment</h2>
    {{> comments._form }}

    <section data-role="comments">
        {{#post.comments}}
            <div>
                {{> comments._comment }}
            </div>
        {{/post.comments}}
    </section>
</div>

JSON Output shows that there are both posts and comments included within post.

{"id":"2","title":"Test Post","content":"Lorem ipsum Reprehenderit velit est irure in enim in magna aute occaecat qui velit ad.","author_name":"Conar Welsh","created_at":"2013-06-23 16:09:45","updated_at":"2013-06-23 16:09:45","comments":[{"id":"3","content":"a","author_name":"a","post_id":"2","created_at":"2013-06-23 16:10:01","updated_at":"2013-06-23 16:10:01"},{"id":"2","content":"Lorem ipsum Nisi dolore ut incididunt mollit tempor proident eu velit cillum dolore sed","author_name":"Testy McTesterson","post_id":"2","created_at":"2013-06-23 16:09:45","updated_at":"2013-06-23 16:09:45"}]}

BUT..... when I changed the debug code from {{{post}}} to {{{post.comments}}}

Laravel showed orange screen with this error.

Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string


 if ($partial = $this->mustache->loadPartial('comments._form')) {
            $buffer .= $partial->renderInternal($context, ' ');
        }
        $buffer .= $indent . '  <pre>';
        $buffer .= "\n";
        $buffer .= $indent . '  ';
        $value = $this->resolveValue($context->findDot('post.comments'), $context, $indent);
        $buffer .= $value;
        $buffer .= "\n";
        $buffer .= $indent . '  </pre>';

How should I solve the Eloquent's error?


I tried to do some tricks to avoid the bug, and it worked. But it seemed causing inefficiency of querying $comments twice.

(both $posts and $comments shared the same comments data, and the Laravel's ORM feature is not being utilised.)

Route::get('posts/{id}', function($id)
{
    $post = App::make('PostRepositoryInterface')->findById($id);

    $comments = App::make('CommentRepositoryInterface')->findAll($post->id);
    return View::make('layouts.application')->nest('content', 'posts.show', array(
        'post' => $post,
        'comments' => $comments,
    ));

});
<article>
    <h3>
        {{ post.title }} {{ post.id }}
        <small>{{ post.author_name }}</small>
    </h3>
    <div>
        {{ post.content }}
    </div>
</article>

<div>

    <h2>Add A Comment</h2>
    {{> comments._form }}

    <section data-role="comments">
        {{#post.comments}}
            <div>
                {{> comments._comment }}
            </div>
        {{/post.comments}}
        {{#comments}}
            <div>
                {{> comments._comment }}
            </div>
        {{/comments}}
    </section>
</div>

Thank you for your time for reading this!

conarwelsh commented 11 years ago

I had some people requesting a change to the way we insert the properties into the mustache templates, so I made a change to the package, that was the reason this functionality broke for you. I reverted back the change, my apologies, if you do an update your code should now work properly.

humuslokiy commented 11 years ago

thank you very much!!