Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.1 eloquent select with prefix joined table

I have this query

$sales = Sales::join('customer', 'customer.id', '=', 'sales.customer_id')
->join('cashier', 'cashier.id', '=', 'sales.cashier_id')
->first();

how can i select each table with prefix so i can call like this :

  • $order->customer_name
  • $order->cashier_name

with my query above, i only get one name row because each table have same column name name, what i want is to give prefixed in each table i call like customer_, cashier_, sales_

Update

the result what i expect is like this

customer_name
customer_address
customer_phone
cashier_name
cashier_another_column
cashier_another_column2
sales_date
sales_another_column
sales_another_column2
like image 307
martiendt Avatar asked Jun 06 '16 05:06

martiendt


1 Answers

the answer is you cannot do that. i usually only alias the same column name and get the rest with *. so your required to alias all same column name for solve the conflict

Sales::join('customer', 'customer.id', '=', 'sales.customer_id')
->join('cashier', 'cashier.id', '=', 'sales.cashier_id')
->select(['*', 'customer.name as customer_name', 'cashier.name as cashier_name'])
->get();
like image 167
Viony Veronica Avatar answered Sep 28 '22 03:09

Viony Veronica