select all columns which are not in another table laravel 5.5

Try it using Eloquent:

$courseUserNames = BuyCourses::pluck('user_name')->all();
$users = User::whereNotIn('user_name', $courseUserNames)->select(...)->get();

Or if you prefer using DB query:

$courseUserNames = DB::table('buy_courses')->pluck('user_name')->all();
$users = DB::table('users')->whereNotIn('user_name', $courseUserNames)->select(...)->get();

DB::table("users")->select('*')->whereNotIn('user_name',function($query) {

   $query->select('user_name')->from('buy_courses');

})->get();

just join actually is inner join in Laravel so actually maybe also you can try:

DB::table('users')
            ->join('buy_courses', 'users.user_name', '=', 'buy_courses.user_name')
            ->get();

Tags:

Mysql

Php

Laravel