dimanche 27 mars 2016

How can I list Forum Categories with its topics in Laravel

I am building a forum in laravel. I want to list each forum category with its topics as so:

Category 1
    -Topic 1 
    -Topic 2 
    -Topic 2
Category 2
    -Topic 1
    -Topic 2
    -Topic 3
But I am getting this in stead
Category 1
  -Topic 1
Category 2
  -Topic 1

This is my select query in my controller

$this->categories = DB::table('forum_categories')
        ->leftjoin('forum_topics','category_id','=','forum_categories.id')
        ->leftjoin('forum_posts','topic_id','=','forum_topics.id')
        ->orderby('category_name','ASC')
        ->get();

This is my Forum_categories model

    public function forum_post()
  {
    return $this->hasMany('App\Forum_topic','category_id');
  }

My Forum_topic model

public function post()
    {
        return $this->hasMany('App\Forum_post','id');
    }
Forum_post model
public function topic()
    {
        return $this->belongsTo('App\Forum_topic','id');
    }

I have also tried join for left join with similar results. Any assistance please.

My view

@foreach($categories as $category)
<h2> {{$category->category_name}}</h2>
-<p>{{$category->topic_subject}}</p>
@endoforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire