EmJeiEn
EmJeiEn

Reputation: 1443

Laravel where with 2 tables

I'm trying to check the another table to remove the matches from the results but unable to figure this out.

$value = people::select(array('people.blog_id'))    
         ->join('blocks', 'people.user_id', '=', 'blocks.blocker')
         ->where('people.user_id', $user->id)
         ->where('blocks.blocker',  '!=', 'people.user_id')
         ->get()
         ->toArray();

What I am trying to achieve, is to strip away the results when getting user_id from people where blocker is found as well in the blocks table, but the following returns an empty array.

Upvotes: 1

Views: 91

Answers (2)

Erfan Ahmed
Erfan Ahmed

Reputation: 1613

As per laravel doc

You may use the table method on the DB facade to begin a query. The table method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally get the results using the get method.

Change your query statement like bellow-

$articles = DB::table('people')
            ->join('blocks', 'people.user_id', '=', 'blocks.blocker')
            ->where('blocks.blocker', '<>', 'people.user_id')
            ->select('people.blog_id')
            ->get();

Upvotes: 1

Raj
Raj

Reputation: 421

I think you should use right join here instead of simple join, You can do it like.

$value = people::select(array('people.blog_id'))
    ->join('blocks', 'people.user_id', '=', 'blocks.blocker', 'right')
    ->where('people.user_id', $user->id)
    ->get()->toArray();

Please notice the fourth parameter in the join statement, this will include only the results where blocks will find. Hope this will help

Upvotes: 0

Related Questions