A
A
Analka2020-01-27 13:08:36
Laravel
Analka, 2020-01-27 13:08:36

Do not list users that are in another Laravel Mysql table?

there is a table ignored

dialog_id | user_id
as to my request, add a condition so as not to display in dialogs correspondence with those people whom I added to

my request ignored

$dialogs = DB::table('dialogs')
                ->leftJoin('users', function ($join) {
                    $join->on('users.id', '=', 'dialogs.recipient_id')
                        ->where('dialogs.user_id','=', Auth::user()->id)
                        ->orOn('users.id', '=', 'dialogs.user_id')
                        ->where('dialogs.recipient_id','=', Auth::user()->id);
                })
                ->leftJoin('user_attributes','user_attributes.user_id','=','users.id')

                ->select('user_attributes.fullname','user_attributes.avatar','user_attributes.age','users.email','dialogs.id','users.id as user')
                ->where('dialogs.user_id','=', Auth::user()->id)
                ->orWhere('dialogs.recipient_id','=', Auth::user()->id)->distinct('dialogs.id')
                ->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Analka, 2020-01-27
@Analka

Wrote like this

$dialogs = DB::table('dialogs')
                ->leftJoin('users', function ($join) {
                    $join->on('users.id', '=', 'dialogs.recipient_id')
                        ->where('dialogs.user_id','=', Auth::user()->id)
                        ->orOn('users.id', '=', 'dialogs.user_id')
                        ->where('dialogs.recipient_id','=', Auth::user()->id);
                })
                ->leftJoin('user_attributes','user_attributes.user_id','=','users.id')

                ->select('user_attributes.fullname','user_attributes.avatar','user_attributes.age','users.email','dialogs.id','users.id as user')
                ->where('dialogs.user_id','=', Auth::user()->id)
                ->whereNotExists(function ($query) {
                    $query->select(DB::raw(1))
                        ->from('dialog_ignores')
                        ->whereRaw('dialog_ignores.dialog_id = dialogs.id')
                        ->whereRaw('dialog_ignores.user_id = '.Auth::user()->id.'')
                        ->whereRaw('dialog_ignores.ignore_user_id = dialogs.recipient_id');
                })
                ->orWhere('dialogs.recipient_id','=', Auth::user()->id)->distinct('dialogs.id')
                ->whereNotExists(function ($query) {
                $query->select(DB::raw(1))
                    ->from('dialog_ignores')
                    ->whereRaw('dialog_ignores.dialog_id = dialogs.id')
                    ->whereRaw('dialog_ignores.user_id = '.Auth::user()->id.'')
                    ->whereRaw('dialog_ignores.ignore_user_id = dialogs.user_id');
            })
                ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question