php - How to echo array elements in laravel

786

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 ) )
150

Answer

Solution:

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

Answer

Solution:

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

Answer

Solution:

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

579

Answer

Solution:

You can convert it into array usingtoArray() 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)

384

Answer

Solution:

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

Simple.

12

Answer

Solution:

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

@foreach($next_d_dts as $next )

 {{  $next }}

@endforeach

People are also looking for solutions to the problem: session - PHP ini change local value

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.