简体   繁体   中英

How to echo array elements in laravel

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 ) )
@foreach($next_d_dts as $value)
{{$value['next_due_bill_date']}}
@endforeach

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;
}

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

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)

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

Simple.

you need to use Blade Loops syntax invoice.view.blade.php

@foreach($next_d_dts as $next )

 {{  $next }}

@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM