1
votes

I have two table content,category

1-Category table is two column name and cat_id

2-content has column title,name,cat_id,id

3-Suppose content has three multiple cat_id

4 now I have to fetch name from that value which is in category table

Please see Attached photo of content and category tableenter image description hereenter image description here

First pic is of content table

Second pic is of category table

public function AllStories()
{
    $cats = DB::table('content')
        ->join('category', 'content.Genres', '=', 'category.Genres')

        ->get();

    $AllStories=Content::all();

    return View::make('AlLStories')->with('AllStories',$AllStories)->with('cats',$cats);
}

now I have to fetch name from that value which is in category table

1
Rethink your schema, it's just wrong. You have to join tables with foreign keys, not comma-separated values.Elon Than
We can't find name of category from values separated by commaHimanshu Dwivedi

1 Answers

0
votes

Your table structure is wrong

If you want to store Category information for a user you need to have pivot table where you can have category id against each content id.

Currently your content table have category as string since it's comma separated where as your category table have id as integer that's the reason you can not join them.

Table Structure

Author: id, author_name, ...

Category: id, category_title, Genre, ....

Content: id, title, ....

Content_Category: content_id, category_id

Author_Content: author_id, content_id

other version of pivot table could be (depending on your requirement)

Content_Category_Author: author_id, content_id, category_id

Now if you have relation like these you can do all kind of joins to fetch any require data.