0

the typical join that looks like this:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

is well documented in the laravel docs here like so:

$this join(string $table, string $one, string $operator = null, string $two = null, string $type = 'inner', bool $where = false)

However, I have no idea where this kind of join is documented:

DB::table('users')
        ->join('contacts', function ($join) {
            $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
        })
        ->get();

for example selectSub clearly has a closure in its documentation:

Builder|Builder selectSub(Closure|Builder|string $query, string $as)

background: trying to figure the syntax out here

abbood
  • 23,101
  • 16
  • 132
  • 246

2 Answers2

0

Go to this link and then search the document for Advanced join clauses or just scroll down a bit, and you will see that header

Nikola Gavric
  • 3,507
  • 1
  • 8
  • 16
0

if there is a relationship with your tables, you can use whereHas method.

$stuff = User::whereHas('contacts', function ($query) {
    $query->where('user_id', '=', $id);
})->get()