我正在处理两个表。一个叫做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
在这种情况下,尝试打印时将返回错误: 此收藏实例上不存在属性[accion]。
我知道此错误是因为详细信息是一个集合。因此,它没有获得action参数,其余的都没有。
我了解可以通过执行foreach to $logs->details as $detail
解决此问题,但这会使整个电路板变形,看起来像这样
Deformed table by nested 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 ▶}
]
}
答案 0 :(得分:0)
我认为您已经定义了一对多关系,因此您必须再次循环以显示所有关系。您确定这是您想要的关系,而不是一对一的关系吗? 看一下laravel文档中的关系。 laravel relationships
这里也有错字
<td>{{$log->details->varialbe}}</td> ->It doesn't work
它是您在迁移中指定的变量
$table->string('variable');