dimanche 25 février 2018

Creating nested comments in laravel

I have the following blog control

public function show($slug)
{
    $post = Blog::where('slugs', '=', $slug)->first();

    $vars['pageTitle'] = Config::get('site.pageTitle') . $post['title'];

    // The breadcrumbs... needs to be repopulated every page
    $vars['breadCrumbs'] = [[
        'url'   => action('SimpleController@index'),
        'title' => 'CovertDEV'
    ],[
        'url'   => action('BlogController@index'),
        'title' => 'Blog'
    ],[
        'url'   => route('blog_post', ['slug' => $slug]),
        'title' => $post['title']
    ]];

    $vars['blog'] = $post;

    // Using the query builder to collect the required data
    $comments = DB::table('blog_comments')
                            ->join('users', 'users.id', '=', 'blog_comments.user_id')
                            ->select(['blog_comments.id',
                                      'blog_comments.comment_id',
                                      'blog_comments.comment',
                                      'blog_comments.likes',
                                      'blog_comments.created_at',
                                      'users.email',
                                      'users.avatar',
                                      'users.firstname',
                                      'users.lastname'])
                            ->where('blog_id', '=', $post['id'])
                            ->get();

    $vars['comments'] = Blog_comment::reArray(collect($comments)->groupBy('comment_id')->toArray());

    return view('blog', $vars);
}

And the following scary view for the comments

@php
function display_comments($main, $depth = "\t")
{
    foreach($main[0] as $mcomment)
    {
@endphp
                                <div class="media">
                                    <img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$mcomment->firstname;?> <?=$mcomment->lastname;?>'s Avatar">
                                    <div class="media-body">
                                        <h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$mcomment->firstname;?> <?=$mcomment->lastname;?></h6>
                                          <div class="p-2">
                                            <p><?=$mcomment->comment;?></p>
                                        </div>
@php
        if(isset($main[$mcomment->id]))
        {
            display_child($main, $main[$mcomment->id], $depth);
        }
@endphp
                                    </div>
                                </div>
@php
    }
}

function display_child($main, $comment, $depth)
{
    foreach($comment as $ccomment)
    {
@endphp
                                        <div class="media">
                                            <img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$ccomment->firstname;?> <?=$ccomment->lastname;?>'s Avatar">
                                            <div class="media-body">
                                                <h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$ccomment->firstname;?> <?=$ccomment->lastname;?></h6>
                                                <div class="p-2">
                                                    <p><?=$ccomment->comment;?></p>
                                                </div>
@php
        if(isset($main[$ccomment->id]))
        {
            display_child($main, $main[$ccomment->id], $depth."\t");
        }
@endphp
                                            </div>
                                        </div>
@php

    }
}

display_comments($comments);

@endphp

That is as ugly as it gets. It generates how I want it, but that is ugly, terribly ugly.

This is an image of the created nested comments

What is the best way to make this work elegantly in the blade template? I tried extending the blade template thing according to the docs, but that didn't prove fruitful for me. I simply couldn't figure out how to do it.

With this, I can't call asset, or anything like that... those are already out of scope.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2GItD2q
via IFTTT

Aucun commentaire:

Enregistrer un commentaire