Laravel update() 在 foreach 中不工作,即使我声明了 $fillable
Laravel update() not working in a foreach even though I have declared $fillable
我这里有一些相当难看的代码:
foreach($request->assigned['standards'] as $standard){
if(@$standard['plannings']) {
foreach($standard['plannings'] as $p) {
dump($p['id']);
$assigned = PerformanceManagementSetModuleStandardAssignedPlanning::
where('id', $p['id'])
->where('performance_management_set_assigned_id', $request->assigned_id)
->first();
$assigned->text = $p['text'];
$assigned->user_id= 1; //I did this to check that the model was updating, it was
dump($assigned);
$assigned->update();
}
}
}
记录正在更新,除“文本”栏外的所有内容。我手动更新了“user_id”列只是为了确保,这个更新非常好。
我有可填写的文字 属性:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PerformanceManagementSetModuleStandardAssignedPlanning extends Model
{
use HasFactory;
protected $fillable = [
'text',
];
}
我转储的时候也可以看到这里(dump($assigned)):
App\Models\PerformanceManagementSetModuleStandardAssignedPlanning {#1611
#fillable: array:1 [
0 => "text"
]
#connection: "mysql"
#table: "performance_management_set_module_standard_assigned_plannings"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:7 [
"id" => 307
"performance_management_set_module_standard_id" => 92
"performance_management_set_assigned_id" => 110
"user_id" => 233
"text" => "134523453452345345234123"
"created_at" => "2022-05-12 10:06:24"
"updated_at" => "2022-05-12 10:28:14"
]
#original: array:7 [
"id" => 307
"performance_management_set_module_standard_id" => 92
"performance_management_set_assigned_id" => 110
"user_id" => 233
"text" => null
"created_at" => "2022-05-12 10:06:24"
"updated_at" => "2022-05-12 10:28:14"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
这里有什么我遗漏的简单的东西吗?
您可以使用 save() 方法进行该更新。
foreach($request->assigned['standards'] as $standard){
if(@$standard['plannings']) {
foreach($standard['plannings'] as $p) {
$assigned = PerformanceManagementSetModuleStandardAssignedPlanning::
where('id', $p['id'])
->where('performance_management_set_assigned_id', $request->assigned_id)
->first();
$assigned->text = $p['text'];
$assigned->user_id= 1; //I did this to check that the model was updating, it was
$assigned->save();
dump($assigned);
}
}
}
原来循环是什么覆盖,每次都传递相同的ID。感谢您的帮助!
我这里有一些相当难看的代码:
foreach($request->assigned['standards'] as $standard){
if(@$standard['plannings']) {
foreach($standard['plannings'] as $p) {
dump($p['id']);
$assigned = PerformanceManagementSetModuleStandardAssignedPlanning::
where('id', $p['id'])
->where('performance_management_set_assigned_id', $request->assigned_id)
->first();
$assigned->text = $p['text'];
$assigned->user_id= 1; //I did this to check that the model was updating, it was
dump($assigned);
$assigned->update();
}
}
}
记录正在更新,除“文本”栏外的所有内容。我手动更新了“user_id”列只是为了确保,这个更新非常好。
我有可填写的文字 属性:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PerformanceManagementSetModuleStandardAssignedPlanning extends Model
{
use HasFactory;
protected $fillable = [
'text',
];
}
我转储的时候也可以看到这里(dump($assigned)):
App\Models\PerformanceManagementSetModuleStandardAssignedPlanning {#1611
#fillable: array:1 [
0 => "text"
]
#connection: "mysql"
#table: "performance_management_set_module_standard_assigned_plannings"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:7 [
"id" => 307
"performance_management_set_module_standard_id" => 92
"performance_management_set_assigned_id" => 110
"user_id" => 233
"text" => "134523453452345345234123"
"created_at" => "2022-05-12 10:06:24"
"updated_at" => "2022-05-12 10:28:14"
]
#original: array:7 [
"id" => 307
"performance_management_set_module_standard_id" => 92
"performance_management_set_assigned_id" => 110
"user_id" => 233
"text" => null
"created_at" => "2022-05-12 10:06:24"
"updated_at" => "2022-05-12 10:28:14"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
这里有什么我遗漏的简单的东西吗?
您可以使用 save() 方法进行该更新。
foreach($request->assigned['standards'] as $standard){
if(@$standard['plannings']) {
foreach($standard['plannings'] as $p) {
$assigned = PerformanceManagementSetModuleStandardAssignedPlanning::
where('id', $p['id'])
->where('performance_management_set_assigned_id', $request->assigned_id)
->first();
$assigned->text = $p['text'];
$assigned->user_id= 1; //I did this to check that the model was updating, it was
$assigned->save();
dump($assigned);
}
}
}
原来循环是什么覆盖,每次都传递相同的ID。感谢您的帮助!