0

I need all columns from invoice,invoice_details table but I want to get only one column (column name: amount) from my payment table. What will be my query for it? here is my controller.

$all_invoice = DB::table('invoice')
        ->join('invoice_details', 'invoice.invoice_id', '=', 'invoice_details.invoice_id')
        ->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
        ->where('invoice.client_id', 7)
        ->orderBy('invoice.invoice_id', 'DESC')
        ->get();
sharif ahmed
  • 13
  • 1
  • 7
  • then just use select, DB::table('invoice')->select("payment.amount") – Kelvin Jun 18 '20 at 07:09
  • Does this answer your question? [Eloquent get only one column as an array](https://stackoverflow.com/questions/34912265/eloquent-get-only-one-column-as-an-array) – glinda93 Jun 18 '20 at 09:36

2 Answers2

0

This very simple you need to add select clause in query like this.

$all_invoice = DB::table('invoice')->select(['invoice.*', 'payment.amount']) ->join('invoice_details','invoice.invoice_id','=','invoice_details.invoice_id') ->join('payment','invoice.invoice_id','=','payment.invoice_id')->where('invoice.client_id',7)->orderBy('invoice.invoice_id','DESC')->get();

If have still query the see the laravel docs in select option in query.

0

Just using select

$all_invoice = DB::table('invoice')
       ->join('invoice_details', 'invoice.invoice_id','=','invoice_details.invoice_id')
       ->join('payment', 'invoice.invoice_id', '=', 'payment.invoice_id')
       ->where('invoice.client_id', 7)
       ->select('payment.amount')
       ->orderBy('invoice.invoice_id', 'DESC')
       ->get();
  • it only gets the amount column.but I need all columns from invoice,invoice_details table but only amount from payment table – sharif ahmed Jun 18 '20 at 07:53
  • ->select('invoice.*','invoice_details.*','payment.amount') If you have the same column names, you should give a different name using 'as' – sinemunll Jun 18 '20 at 08:11