php laravel 5.2中未找到replicate()方法

sigwle7e  于 2022-12-10  发布在  PHP
关注(0)|答案(4)|浏览(141)

我正在尝试复制表行及其关系。
但我收到了replicate()不存在的错误消息,

我在stackoverflow上看到很多人使用replicate()没有任何问题,但是我得到了这个错误
我的控制器代码

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;

}

有没有任何名称空间我必须使用replicate(),我无法从laravel网站也得到解决方案。
请帮忙。

dkqlctbz

dkqlctbz1#

您可以在模型上使用replicate(),但不能在集合上使用。
通过使用get()获取记录,您将返回一个集合。
如果您只希望返回一条记录,则将get()替换为first(),然后replicate()应该存在,因为它将返回模型的示例而不是集合:

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;
}

您还将需要save()$newshowtime

lymnna71

lymnna712#

这段代码对我来说非常有效

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

}

如有任何改进此代码的建议,我们将不胜感激。

eulz3vhy

eulz3vhy3#

这种类型的函数将有助于克隆多个记录,并将这些记录添加到同一个表中。我尝试了类似的代码流,并成功了。

/**
* 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();
        }
    }
}

干杯!干杯!

iqxoj9l9

iqxoj9l94#

您可以轻松地复制具有新更改的行

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

如果不使用toArray(),则foreach循环中的每个元素都将是Eloquent对象。

相关问题