Alex
Alex

Reputation: 79

How to implode an array of arrays in Laravel?

Hi I have the following query:

$inc_quotes = DB::table('inc_quotes')->where('session_id', '=', $session_id)->get();

And it returns an array of arrays I believe like below:

    array:36 [▼
  0 => {#232 ▼
    +"id": "5"
    +"session_id": "1ee0556134d377c05673fce16f719b3e1077c797"
    +"brand": "Acer"
    +"drive": "Full Size Laptop"
    +"screen": "Less than 2 Years old"
    +"processor": "AMD A6"
    +"condition": "No"
    +"faults": "Light Damage,Heavy Damage"
    +"price": "16.37"
    +"name": "Alex"
    +"lastname": "C"
    +"email": "[email protected]"
    +"mobile": "12344567"
    +"created_at": "2016-02-20 09:05:51"
    +"updated_at": "2016-02-20 09:05:51"
  }
  1 => {#233 ▶}
  2 => {#234 ▶}

Now I would like to extract from each array(row) for example the name and do a for each on the view to show the names for each row.

How can I extract the values?

I tried this

foreach($inc_quotes as $quote){
        $quote_name = $quote->name;
    }

But only returns the last value.

Any help much appreciated.

Upvotes: 0

Views: 1068

Answers (2)

utsav
utsav

Reputation: 622

$quote_name = [];
foreach($inc_quotes as $key => $quote){
        $quote_name[$key] = $quote->name;
    }

EDITED

pass this to view like this.

return view('whateverYourView',compact('quote_name'));

and in view you access it with

@foreach($quote_name as $name)
{{ $name }}
@endforeach

Upvotes: 3

Albert Abdonor
Albert Abdonor

Reputation: 961

Your $quote_name must be an array or an collection to receive all data that is comming from the array that you are iterating. The $quote_name will be subscribed in every loop, that's why the method it will show only the last result.

Upvotes: 0

Related Questions