Back

Retrieving latest entry from recursive relationship


jtPox posted 4 months ago

I am not sure if the title is phrased correctly, but I will try to explain it here. For context, here is the database structure that I am using.

-forums-

-id

-name

-description

-user_id

-type (1 = category, 2 = forum, 3 = thread, 4 = post)

-parent

A forum can have a multiple sub-forums (even recursively - sub-sub-forum). I need a way to the latest post (or thread if post does not exist) and display it on the homepage.

Here are the codes that I currently have:

Model:

public function user()
{
    return $this->hasOne('App\User', 'id', 'posted_by');
}

public function child()
{
    return $this->hasMany('App\Forums', 'parent', 'id');
}

public function parent()
{
    return $this->hasOne('App\Forums', 'id', 'parent');
}

/*
 * Relations for listing.
 */
public function forums()
{
    return $this->hasMany('App\Forums', 'parent', 'id')->where('type', '=', 2)->orderBy('place', 'asc');
}

public function threads()
{
    return $this->hasMany('App\Forums', 'parent', 'id')->where('type', '=', 3)->orderBy('created_at', 'desc');
}

public function posts()
{
    return $this->hasMany('App\Forums', 'parent', 'id')->where('type', '=', 4)->orderBy('created_at', 'desc');
}

Controller:

$forum = Forums::where('type', '=', 1)->get();

View:

@foreach($forum as $cat)
<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">{{ $cat['name'] }} <small>{{ $cat['description'] }}</small></h4>
    </div>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Forums</th>
                <th></th>
                <th class="text-right">Latest Thread</th>
            </tr>
        </thead>    
        <tbody>
            @foreach($cat['child'] as $forums)
            <tr style="width:50%">
                <td>
                    <p>
                        <a href="#/forum/{{ $forums['id'] }}">{{ $forums['name'] }}</a>
                    </p>
                    <p>
                        {{ $forums['description'] }}
                    </p>
                </td>
                <td class="text-center" style="width:10%">
                    <p>
                        <span class="text-count"></span><br />
                        Discussions
                    </p>
                </td>
                <td class="text-right" style="width:40%">
                    @foreach($forums['threads'] as $thread)
                    <p>
                        <img src="{{ route('User.Avatar', ['id' => $thread->user['id']]) }}" alt="{{ $thread->user['username'] }}'s Avatar" class="img-circle" height="30px" />
                        <a href="#/thread/{{ $thread['id'] }}">{{ $thread['name'] }}</a>
                    </p>
                    <p>
                        <a href="#/user/profile/{{ $thread->user['id'] }}">{{ $thread->user['username'] }}</a>, {{ date('M j, Y', strtotime($thread['created_at'])) }}
                    </p>
                    @endforeach                 
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
@endforeach

I have also tried the method with(), however, it does not follow the parent-child relationship when seperated into categories and forums.

I would appreciate any help given.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us