dollar dollar - 2 months ago 23
PHP Question

replicate() method not found in laravel 5.2

I am trying to replicate table row and its relationship.

but I am getting error message that replicate() does not exist,

enter image description here

I have seen on stackoverflow that many have used replicate() without any issue, but i am getting this error

my controller code

public function copyshowtime($cinema_id,$show_date)
{
$date=new Carbon($show_date);
$current_show_date=$date->format('Y-m-d');
$next_show_date=$date->addDay()->format('Y-m-d');

$movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->get();

$newshowtime=$movieshowtime->replicate();
return $newshowtime;


}


Is there any namespace i have to use for using replicate() , I am unable to get solution from laravel website also.

help is appreciated.

Answer

You can use replicate() on a model but not on a collection.

By fetching your records using get() you are returning a collection.

If you are just expecting one record to be returned then replace get() with first() and then replicate() should exist as it will be returning an instance of the model rather than a collection:

public function copyshowtime($cinema_id,$show_date)
{
    $date=new Carbon($show_date);
    $current_show_date=$date->format('Y-m-d');
    $next_show_date=$date->addDay()->format('Y-m-d'); 

    $movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->first();

    $newshowtime=$movieshowtime->replicate();
    return $newshowtime;
}

You will also need to save() the $newshowtime.