How to edit items in laravel collection

26,072

Solution 1

If you want to do this transformations always for your model, you can just add the following accessor method to the model class:

public function getNumbertemplateAttribute() {
  return str_replace('x', '-', $this->attributes['numbertemplate']);
}

Now everytime you access $customerCallPlan->numbertemplate you will get the converted string.

Otherwise just convert the column when you fetch the data:

$plans = $callPlans->get()->map(function($plan) {
  $plan->numbertemplate = str_replace('x', '-', $plan->numbertemplate);
  return $plan;
});

Solution 2

You could do the following:

$callPlans = CustomerCallPlan::whereNotNull('id')->get();

foreach ($callPlans as $callPlan) {
    $callPlan->numbertemplate = (whetever you need);
    $callPlan->save(); //save the changes
}

Hope this was helpful.

Solution 3

You could use update() and str_replace() :

$callPlans = CustomerCallPlan::whereNotNull('id');

foreach ($callPlans->get() as $callPlan) {
    $callPlan->update(["numbertemplate"=>str_replace("x", "-", $callPlan->numbertemplate]);
}

Hope this helps.

Solution 4

By default php creates a copy of a variable when passing it to a function as a parameter. You can override this behaviour by prefixing the parameter with an ampersand, which will pass a pointer to the original variable to the function instead.

We can use this in a foreach loop inside the collection to modify the original item like so:

$callPlans->each(function(&$plan) {
    $plan->numbertemplate = 'xyz';
});
Share:
26,072
TyForHelpDude
Author by

TyForHelpDude

I'm here as a lifelong learner , I thank all the members the Stackoverflow's community for the great help that comes to me and I try in my turn to give my little help as I can.

Updated on October 20, 2020

Comments

  • TyForHelpDude
    TyForHelpDude over 3 years

    I am new in laravel, I run a query and get rows from database and I want to edit a column of this rows before get them in view. So here is my code piece :

    $callPlans = CustomerCallPlan::whereNotNull('id');
    
    foreach ($callPlans->get() as $callPlan) {
        dd($callPlan);
    }
    

    And the output screenshot:

    enter image description here

    I need to replace all the 'x' characters with '-' of numbertemplate column..

  • Nady Shalaby
    Nady Shalaby over 6 years
    This is not recommended for large collection size so think of it if i am using some sort of pagination. and i want to do some processing without losing the paginated collection in other words i only want to process the fetched items per request. i recommend editing items passed by reference
  • Adam Kozlowski
    Adam Kozlowski over 5 years
    do not forget to return $plan
  • Nico Haase
    Nico Haase over 5 years
    Why should that be neccessary?
  • Chad
    Chad about 5 years
    Because otherwise, the ->map() method will return null, and in my experience today, that's exactly what each fetched model instance will be(come). Before the end of the code block, add return $varName; to fix this issue.