I have some rather ugly code here:
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();
}
}
}
The record is updating, everything apart from the "text" column. I manually updated the "user_id" column just to make sure, and this updates perfectly fine.
I have text as a fillable property:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PerformanceManagementSetModuleStandardAssignedPlanning extends Model
{
use HasFactory;
protected $fillable = [
'text',
];
}
It can also be seen here when I dump ( 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 => "*"
]
}
Is there something simple I'm missing here?
CodePudding user response:
You can use save() method for that update.
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);
}
}
}
CodePudding user response:
Use save()
instead update()
. You can check the doc here about how the update and save works in Laravel.
Change from
$assigned->update();
to
$assigned->save();
https://laravel.com/docs/9.x/eloquent#updates
CodePudding user response:
Turns out the loop what overwriting, the same ID was being passed each time. Thanks for all your help!