Packy Packy - 1 month ago 15
PHP Question

Laravel 5.2 DB Query

I am trying to use

DB
to build a query like so:

Get all
Distributors
belonging to the
User
with their
Accounts
that have
Notes
.

I cant wrap my head around that query. Here is what I had that works in that it gets spits out the info I need in one array but 2 issues: the
note
name
overwrites the
account
name
since they are both called that, and I cant pass in a
$user->
to replace the
2


Working (kind of)

$user = Auth::user();

$accounts = DB::table('distributors')
->join('accounts', function ($join) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', 2);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name', 'notes.*')
->get();


What I need

$user = Auth::user();

$accounts = DB::table('distributors')
->join('accounts', function ($join, $user) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', $user->id);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name (this is overwritten. can I change it to come out account_name so it doesnt?)', 'notes.*')
->get();

Answer

Try this for account name:

  ->select('accounts.name as custom_account_name')

Hope this helps.