Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel replicate() function is not working for model collection

i have facing a problem at laravel.

I want to replicate/clone of collection after apply filter the collection.

code is below:

/* Scope function */         
    $plansFilterIndexes=Plans::PlansFilterFields();
     $planFilters=Plans::FilterFieldsSerial($query);
     /* Create Plans collection */

     $PlansAll=Plans::all();

    /* Plans Filter by url 
     * here checking $planFilter  indexes IS AVALIABLE AT 
     *  DEFINED  INDEX $plansFilterIndexes
     */
    $query = null;
        foreach($planFilters as $eachIndexKey => $eachIndexValue):
        echo $eachIndexKey.'------'.$eachIndexValue.'<br/>';
        if(array_search($eachIndexKey, $plansFilterIndexes) != false):

            $PlansAll = $PlansAll->where($eachIndexKey, $eachIndexValue);
        endif;
    endforeach;
    //dd($PlansAll);
    /* Clone collection into new once for copy */

    $PlansAllClone =$PlansAll->replicate();
    dd($PlansAllClone);

Try with Clone an Eloquent object including all relationships? solution but did not get result

Show error: Method replicate does not exist.

It not working.

Question:

Why $PlansAllClone =$PlansAll->replicate(); is failed to give result. and if it not possiable ,show i will achieve it.

like image 216
Amit Bera Avatar asked Feb 03 '26 18:02

Amit Bera


1 Answers

Replicate is a method for cloning a model into a new non existing instance not for cloning a Collection.

replicate is used like this:

$newPlansInstance = Plans::replicate();

It can except an optional argument which is an array of exceptions.

What exactly are you trying to accomplish ?

like image 118
funkenstein Avatar answered Feb 06 '26 08:02

funkenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!