0

I'm working on a laravel 5 application. I have issue getting results from two database table. here is what i have:

table A: 'courses' 

id   |    Course
————————————————
1    |    Math
2    |    History
3    |    Geography
4    |    Computer

and Table B

user_id | classroom_id | course 
1       | 5            | 3
1       | 5            | 4
1       | 6            | 2

I returned the table A on a for each loop but I would like to check what courses the user_id 1 has to return true or false on every column on the for-each loop. Something like this:

Returned item for user_id 1:

id   |    course     |  status
____________________________
1    |    Math       | false
2    |    History    | true
3    |    Geography  | true
4    |    Computer   | true

This is I have:

$AllList = DB::table('users')
            ->join('courses', 'users.id', '=', 'courses.parent_id')
            ->join('classroom', 'users.id', '=', 'classroom.user_id')->where('classroom_id', '=', 5)       
            ->get();

Any help appreciated.

6
  • 1
    A left join should do it, if empty then it's 'false' Commented Sep 20, 2016 at 8:20
  • @ka_lin Thanks for comment but what the code should be? Commented Sep 20, 2016 at 8:23
  • What do you have so far? Commented Sep 20, 2016 at 8:31
  • first you have to create a belongs to relation,then when you query "courses" just check is there any relation with table b then print rather true or false.Please show here what you tried,that we can help you through code or logic Commented Sep 20, 2016 at 8:35
  • select course from A inner join B on A.id=B.couse where B.user_id=1; Commented Sep 20, 2016 at 8:44

2 Answers 2

2

Just replace join with leftJoin:

$AllList = DB::table('courses')
            ->select('courses.id','course.name')
            ->leftJoin('users', 'users.id', '=', 'courses.parent_id')
            ->join('classroom', 'users.id', '=', 'classroom.user_id')->where('classroom_id', '=', 5)       
            ->get();

Course field will be NULL thus empty string if there is no match

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. So how to limit the returned columns?because I have all columns on both tables.
Just add ->select([column names as array]); like I did above
Cool, Thanks. I just voted up you answer and not selecting as the accepted answer to get more answer form other people. Thanks again
the reason why didn't choose as the answer because I still get Null for all the Data: [{"id":null,"name":null},{"id":null,"name":null},{"id":null,"name":null},{"id":null,"name":null}]
Oh sorry, I replaced 'courses with users because there are users with no courses, that is why you are getting empty values
1

the below lines of code will help you...

    $getCourse = DB::table('courses')->get(['id','course']);

    $getCourse = collect($getCourse)->map(function($x){ return (array) $x; })->toArray();

    foreach ($getCourse as $key => $value)
    {
        $flag = DB::table('classroom')
                    ->where('user_id',1)
                    ->where('course',$value['id'])
                    ->pluck('id');
        if($flag)
        {
            $getCourse[$key]['status'] = true;
        }
        else
        {
            $getCourse[$key]['status'] = false;
        }
    }

    dd($getCourse);

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.