0

I'm trying to make simple forum on laravel. I have two tables: categories:

enter image description here

forums:

enter image description here

I run query:

$categories = DB::table('forums')
        ->join('categories', 'forums.fid', '=', 'categories.cid')
        ->select('categories.*', 'forums.*')
        ->get();

I receive only two results:

[{"cid" :1,
  "name": "First forum",
  "fid": 1,
  "seo_name": "first-forum",
  "category_id": 1
 },
 {"cid": 2,
  "name": "Another forum",
  "fid": 2,
  "seo_name": "another-forum",
  "category_id": 2
 }]

Why only 1 result for category_id 1? I have two forums in that category. Thanks in advance and sorry for my bad English.

1
  • You should have spent the time you wasted here on creating this question on debugging. Try to learn debugging on your own. That is the most important thing for a programmer. :) Commented Dec 5, 2017 at 12:31

2 Answers 2

2

You need:

->join('categories', 'forums.category_id', '=', 'categories.cid')
Sign up to request clarification or add additional context in comments.

Comments

1

Update query to:

$categories = DB::table('forums')
        ->join('categories', 'forums.category_id', '=', 'categories.cid')
        ->select('categories.*', 'forums.*')
        ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.