jeudi 28 avril 2016

Laravel Output Associated Tags

I have an application in Laravel 5.1, my application allows the user to add an article and choose a number of categories associated to that article.

I save the relationship between the article in an association table and I now want to output on the user's articles page all the articles the user has submitted but at the side of the title show the categories the article is within.

the articles page will list all the articles so the list has to be unique.

my controller:

public function index()
    {
        $articles = DB::table('articles')
                    ->leftjoin('w_relations','w_relations.article_id','=','articles.id')
                    ->leftjoin('articlecategories','articlecategories.id','=','w_relations.type_id')
                    ->where('articles.comp_id',Auth::user()->comp_id)->get();

        return view('advertiser/workshops', compact('workshops'));
    } 

my view

<ul>
@foreach($articles as $a)
    <li> | </li>
@endforeach
</ul>

I thought the above would output

Article 1 | History, Category, Category2

but instead I get

Article 1 | History Article 
1 | Category Article 
1 | Category2

I can kind of see where I'm going wrong but I don't know how to fix the query?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire