循环收集

时间:2015-05-18 08:07:31

标签: php laravel laravel-5

如果我在视图中对$ payment进行了print_r,那么这就是我的结果;

Array
(
[0] => App\Models\Payment Object
    (
        [connection:protected] => 
        [table:protected] => 
        [primaryKey:protected] => id
        [perPage:protected] => 15
        [incrementing] => 1
        [timestamps] => 1
        [attributes:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => SERVICES AGREEMENT
                [Vendor ZIP] => A123GQ
            )

        [original:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => SERVICES AGREEMENT
                [Vendor ZIP] => A123GQ
            )

        [relations:protected] => Array
            (
            )

        [hidden:protected] => Array
            (
            )

        [visible:protected] => Array
            (
            )

        [appends:protected] => Array
            (
            )

        [fillable:protected] => Array
            (
            )

        [guarded:protected] => Array
            (
                [0] => *
            )

        [dates:protected] => Array
            (
            )

        [casts:protected] => Array
            (
            )

        [touches:protected] => Array
            (
            )

        [observables:protected] => Array
            (
            )

        [with:protected] => Array
            (
            )

        [morphClass:protected] => 
        [exists] => 1
    )

[1] => App\Models\Payment Object
    (
        [connection:protected] => 
        [table:protected] => 
        [primaryKey:protected] => id
        [perPage:protected] => 15
        [incrementing] => 1
        [timestamps] => 1
        [attributes:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => TST
                [Vendor ZIP] => A124YD
            )

        [original:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => TST
                [Vendor ZIP] => A124YD
            )

        [relations:protected] => Array
            (
            )

        [hidden:protected] => Array
            (
            )

        [visible:protected] => Array
            (
            )

        [appends:protected] => Array
            (
            )

        [fillable:protected] => Array
            (
            )

        [guarded:protected] => Array
            (
                [0] => *
            )

        [dates:protected] => Array
            (
            )

        [casts:protected] => Array
            (
            )

        [touches:protected] => Array
            (
            )

        [observables:protected] => Array
            (
            )

        [with:protected] => Array
            (
            )

        [morphClass:protected] => 
        [exists] => 1
    )

[2] => App\Models\Payment Object
    (
        [connection:protected] => 
        [table:protected] => 
        [primaryKey:protected] => id
        [perPage:protected] => 15
        [incrementing] => 1
        [timestamps] => 1
        [attributes:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => ONE
                [Vendor ZIP] => A238JW
            )

        [original:protected] => Array
            (
                [Valued or Running] => R
                [Contract Title] => ONE
                [Vendor ZIP] => A238JW
            )

        [relations:protected] => Array
            (
            )

        [hidden:protected] => Array
            (
            )

        [visible:protected] => Array
            (
            )

        [appends:protected] => Array
            (
            )

        [fillable:protected] => Array
            (
            )

        [guarded:protected] => Array
            (
                [0] => *
            )

        [dates:protected] => Array
            (
            )

        [casts:protected] => Array
            (
            )

        [touches:protected] => Array
            (
            )

        [observables:protected] => Array
            (
            )

        [with:protected] => Array
            (
            )

        [morphClass:protected] => 
        [exists] => 1
    )

我正在尝试将结果打印到表格中。喜欢的东西;

@foreach($payments as $key => $payment)
  <tr>
     @foreach($payment as $subkey => $subpayment)
        <td>{{ $subkey }}</td>
    @endforeach
  </tr>
@endforeach

我不能让我的循环工作!我想打印出三个字段,表中每行一个。

1 个答案:

答案 0 :(得分:2)

我认为您正在寻找attributesToArray方法,该方法将模型的属性转换为关键数组,其中键是属性名称。这样的事情对你有用:

@foreach($payments as $payment)
  <tr>
    @foreach($payment->attributesToArray() as $value)
      <td>{{ $value }}</td>
    @endforeach
  </tr>
@endforeach