Reputation: 1480
I have a raw query like this:
$models = DB::select(DB::raw("SELECT ... etc));
I want to modify the data inside the resulting collection (I don't think it's a collection though).
Anyway let's say I want to modify all the $model->picture to something like 'whatever/path' . $model->picture.
I tried like this and it doesn't modify anything yet it doesn't spit any error:
foreach($models as $model){
$model['picture'] = '/images/models/' . $model['picture'];
}
what am I missing?
Upvotes: 0
Views: 1427
Reputation: 111829
Well, for me you should use Eloquent for this so something like this would be enough:
$models = YourModel::all();
foreach($models as $model){
$model->picture = '/images/models/' . $model->picture;
$model->save(); // here you save it to database
}
However to be honest it doesn't make any sense do add this path to database. You could use accessor for your Eloquent model and assuming you use everywhere Eloquent don't touch database at all.
EDIT
Ok, so for such case (from comment) the problem is that you don't use reference here, you should use it like this:
foreach($models as &$model){
$model['picture'] = '/images/models/' . $model['picture'];
}
unset($model)
to make it really be saved in models array.
Upvotes: 1