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');
//return $this->hasManyThrough('App\Forums', 'App\Forums', 'parent', 'id', 'id');
//return $this->hasManyThrough('App\Forums', 'App\Forums', 'parent', 'parent', 'id');
}
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"> <small></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/"></a>
</p>
<p>
</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="" alt="'s Avatar" class="img-circle" height="30px" />
<a href="#/thread/"></a>
</p>
<p>
<a href="#/user/profile/"></a>,
</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.
via JianTing