0

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.

dollar
  • 483
  • 3
  • 11
  • 27

4 Answers4

1

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.

James
  • 15,754
  • 12
  • 73
  • 91
  • I want to replicate the collection, so replicate will not work on it, then is it better to write the complete code which will copy all those rows from table and insert it again in the table... or is there anything else which i am missing – dollar Sep 22 '16 at 07:11
  • 1
    foreach($collection as $item) { $newItem = $item->replicate() } – Rolf Pedro Ernst Sep 22 '16 at 08:58
  • @dollar as Rolf suggests you could do it like that. In fact there's a few ways you could do it, just make sure you save after you replicate! – James Sep 22 '16 at 09:13
  • thanks for help but one more query i have when i used foreach and replicate, it's copying everything including id , and I am getting duplicate entry error message, so how can i remove the id from replicate data – dollar Sep 23 '16 at 01:13
0

This code worked perfectly for me

 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();

    foreach ($movieshowtime as $item) 
    {
        $item->show_date=$next_show_date;
        $item->show_id=NULL;
        $newshowtime=$item->replicate();
        $newshowtime->push();


        foreach ($item->showdata as $sd) 
        {


            $newshowdata = array(
                                    'showdata_id' => NULL,
                                    'show_id'=>$newshowtime->id,
                                    'category_id'=>$sd->category_id,
                                    'showdata_category'=>$sd->showdata_category,
                                    'showdata_rate'=>$sd->showdata_rate

                                );


           // print_r($newshowdata);
            Movies_showdata::create($newshowdata);



        }
    }

    return redirect()->back();


}

Any suggestions to improve this code will be appreciated.

dollar
  • 483
  • 3
  • 11
  • 27
0

This type of function would help to clone multiple records and add those records in the same table. I tried a similar code flow and worked.

/**
* Clone multiple records in same table
*
* @params int $cinemaId
* @params string $showDate
*
* @return bool $status
*
* @access public
*/
public function copyShowTime($cinemaId, $showDate)
{
    $date = new Carbon($showDate);
    $currentShowDate = $date->format('Y-m-d');

    // Cloned & Create new records
    $moviesShowTimeCollection = Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinemaId],['show_date','=',$currentShowDate]])->get();
    // Please check that Model name should change according to camelCases - Movies_showtimes to MoviesShowtimes
    if(!$moviesShowTimeCollection->isEmpty()) {
        $moviesShowTimeData = $moviesShowTimeCollection->toArray();
        foreach ($moviesShowTimeData as $key => $value) {
            $primaryKey = 'show_id'; // Needs to check the table primary key name
            $primaryId = $value[$primaryKey];
            $moviesShowTimeObj = Movies_showtimes::find($primaryId);
            // below code can modify while cloaning 
            //$clonedMoviesShowTimeObj = $moviesShowTimeObj->replicate()->fill([
            //    'column_name' => $updatedValue
            //]);
            $clonedMoviesShowTimeObj = $moviesShowTimeObj->replicate(); // just to clone a single record
            $status = $clonedMoviesShowTimeObj->save();
        }
    }
}

Cheers!

0

You can easily replicate rows with new changes in that rows

$apcntReplicate = TrademarkApplicantMap::where('trademark_id', $trdIdForPostAssesment)->get();

foreach($apcntReplicate as $oldapnctdata) 
{
   $apcntreplicated = $oldapnctdata->replicate() ;

   //update row data which will newly created by replicate 
   $apcntreplicated->row_name =  $newrowdata;

   //save new replicated row
   $apcntreplicated->save();
}

Don't use toArray() then each element in the foreach loop will be an Eloquent object.

Vinay Kaithwas
  • 1,415
  • 10
  • 17