如何在laravel中回显数组元素

时间:2018-02-15 09:34:03

标签: php arrays laravel

我想在视野中回应arry elemnts 以下是我的代码(控制器)

$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);

我可以使用打印功能打印它,例如:

print_r($next_d_dts); 

输出:

Array ( [0] => stdClass Object ( [next_due_bill_date] => 2019-03-28 ) [1] => stdClass Object ( [next_due_bill_date] => 2020-02-28 ) )

6 个答案:

答案 0 :(得分:1)

您需要iterate over the collection个对象:

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

如果您只想查看其内容但不停止脚本:

{{ var_dump($next_d_dts) }}

您还问过如何在没有Blade的情况下进行迭代:

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

答案 1 :(得分:1)

你应该在这样的laravel中使用foreach循环

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

了解更多信息,请阅读Laravel Manual blade template

你也可以用

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

您可以使用toArray()将其转换为数组并对其进行迭代

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

在视图中

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

或者

print_r($next_d_dts)

答案 4 :(得分:1)

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

简单。

答案 5 :(得分:-1)

您需要使用Blade Loops 语法 invoice.view.blade.php

@foreach($next_d_dts as $next )

 {{  $next }}

@endforeach