How to echo array elements in laravel

10,050

Solution 1

You need to iterate over the collection of objects:

@foreach ($next_d_dts as $object)
    {{ $object->name }}
@endforeach

If you just want to see its contents but not stop the script:

{{ var_dump($next_d_dts) }}

You've also asked how to iterate without Blade:

foreach ($next_d_dts as $object) {
    echo $object->name;
}

Solution 2

@foreach($next_d_dts as $value)
{{$value['next_due_bill_date']}}
@endforeach

Solution 3

you should used foreach loop in laravel like this

@foreach ($next_d_dts as $value)
    <p>Some text here{{ $value->id }}</p>
@endforeach

for more information read Laravel Manual blade template

Also you can used

dd($next_d_dts) //The dd function dumps the given variables and ends execution of the script

Solution 4

You can convert it into array using toArray() and iterate over it

$next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get()->toArray(); 

In view

@foreach($next_d_dts as $value)
{{ $value['column_name'] }}
@endforeach

Or

print_r($next_d_dts)

Solution 5

@foreach($array as $item)
    {{$item}}
@endforeach

Simple.

Share:
10,050

Related videos on Youtube

Rummaan
Author by

Rummaan

Updated on June 04, 2022

Comments

  • Rummaan
    Rummaan almost 2 years

    I want to echo arry elemnts in view followings are my code (controller)

    $next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get(); 
    
    return view('invoice.view')->with('next_d_dts',$next_d_dts);
    

    I can print it using print function, eg:

    print_r($next_d_dts); 
    

    Output:

    Array ( [0] => stdClass Object ( [next_due_bill_date] => 2019-03-28 ) [1] => stdClass Object ( [next_due_bill_date] => 2020-02-28 ) )
    
    • Prashant Prajapati
      Prashant Prajapati about 6 years
      $next_d_dts["key_name"];
    • Sohel0415
      Sohel0415 about 6 years
      this is a collection not an array
  • Rahul
    Rahul about 6 years
    where is 'foreach()'
  • Mahdi Younesi
    Mahdi Younesi about 6 years
    Updated the answer
  • Rummaan
    Rummaan about 6 years
    how to use without blade?