1

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.

Mik_ko
  • 134
  • 2
  • 9
EmJeiEn
  • 1,343
  • 5
  • 17
  • 30
  • Instead of `->where('blocks.blocker', '!=', 'people.user_id')` you need something like `->where('blocks.blocker', '=', 'null')`. https://stackoverflow.com/a/4076157/3585500. I don't know the exact eloquent syntax, but maybe `whereNull('blocks.blocker')` function? – ourmandave Dec 25 '18 at 02:48
  • what do want exactly from your query? , i mean , tell for example 'i want people who hasn't blocked , or ...) and tell me your table structure. – Saman Ahmadi Dec 25 '18 at 06:55

2 Answers2

1

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();
Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34
0

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

Raj
  • 399
  • 3
  • 7