首页 > 解决方案 > 如何在 Laravel 中分解关系集合

问题描述

我正在使用两张桌子。一个叫 transacciones,另一个叫 transaction_details 这是 transacciones 表

Schema::create('transacciones', function (Blueprint $table) {
        $table->increments('id');

        $table->string('modulo');

        $table->timestamps();
    });

这是 transaction_details 表

Schema::create('transaction_details', function (Blueprint $table) {
        $table->integer('transacciones_id')->unsigned();

        $table->string('accion');
        $table->string('variable');
        $table->string('anterior')->nullable();
        $table->string('nuevo')->nullable();

        $table->foreign('transacciones_id')
            ->references('id')
            ->on('transacciones')
            ->onDelete('cascade');

        $table->timestamps();
    });

我正在尝试使用控制器文件中的以下代码传递数据以查看:

public function Transactions()
    {
        $logs = Transacciones::with('details')->get();
        return view('transacciones', compact('logs'));
    }

在模型文件中

@foreach($logs as $key => $log)

<tr>
    <td>{{ $log->id }}</td> //-> Works well

    <td>{{$log->details->accion}}</td> ->It doesn't work
    <td>{{$log->details->variable}}</td> ->It doesn't work
    <td>{{$log->details->anterior}}</td> ->It doesn't work
    <td>{{$log->details->nuevo}}</td> ->It doesn't work

    <td>{{ucfirst( $log->modulo) }}</td> //-> Works well
</tr>
@endforeach

在这种情况下,当尝试打印时返回错误:Property [accion] does not exist on this collection instance。

我知道这个错误是因为details 是一个集合。所以它没有得到动作参数和其他所有参数。我知道这可以通过执行解决,foreach to $logs->details as $detail但这会使我的整个电路板变形,并且看起来像 嵌套 foreach 的变形表

是否可以像 Laravel 文档中那样只使用一个 foreach 来完成所有这些工作?让我们留下这样的东西

<td>{{$log->details->accion}}</td>
<td>{{$log->details->varialbe}}</td>
<td>{{$log->details->anterior}}</td>
<td>{{$log->details->nuevo}}</td>

我是 Laravel 的新手。我只用了很短的时间。我希望你能帮我解决这个问题,事实是我有超过 24 小时的时间遇到​​同样的问题,不知道如何解决。

编辑:添加 dd($logs)

Collection {#1075 ▼
  #items: array:4 [▼
    0 => Transacciones {#913 ▶}
    1 => Transacciones {#914 ▼
      #table: "transacciones"
      #fillable: array:2 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 2
        "modulo" => "Usuarios"
        "created_at" => "2018-11-24 05:53:19"
        "updated_at" => "2018-11-24 05:53:19"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "details" => Collection {#1072 ▼
          #items: array:6 [▼
            0 => Transaction_Detail {#1043 ▼
              #table: "transaction_details"
              #fillable: array:4 [▶]
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:7 [▼
                "transacciones_id" => 2
                "accion" => "<Escribir>"
                "variable" => "nombres"
                "anterior" => "Joaquin Moises"
                "nuevo" => "Isaac Rosalind"
                "created_at" => "2018-11-24 05:53:19"
                "updated_at" => "2018-11-24 05:53:19"
              ]
              #original: array:7 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => Transaction_Detail {#1044 ▶}
            2 => Transaction_Detail {#1045 ▶}
            3 => Transaction_Detail {#1046 ▶}
            4 => Transaction_Detail {#1047 ▶}
            5 => Transaction_Detail {#1048 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    2 => Transacciones {#915 ▶}
    3 => Transacciones {#916 ▶}
  ]
}

标签: phparrayslaravel

解决方案


我认为您已经定义了一对多关系,因此您必须再次循环以显示所有这些关系。你确定这是你想要的关系而不是一对一的关系吗?查看 laravel 文档中的关系。 laravel 关系

此外,您在这里有一个错字 <td>{{$log->details->varialbe}}</td> ->It doesn't work ,因为您在迁移中指定它是可变的 $table->string('variable');


推荐阅读