3

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 ) )
2
  • $next_d_dts["key_name"]; Commented Feb 15, 2018 at 9:36
  • 1
    this is a collection not an array Commented Feb 15, 2018 at 9:39

6 Answers 6

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Comments

1

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)

Comments

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

Simple.

Comments

-1

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

@foreach($next_d_dts as $next )

 {{  $next }}

@endforeach

3 Comments

where is 'foreach()'
Updated the answer
how to use without blade?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.