2016-09-22 139 views
0

我在嘗試複製表格行及其關係。在laravel中找不到replicate()方法5.2

,但我得到的是複製()的錯誤信息不存在,

enter image description here

我看到的計算器,許多人使用複製()沒有任何問題,但我得到這個錯誤

我控制器代碼

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; 


} 

有沒有命名空間,我必須使用使用複製(),我無法得到解決方案也來自laravel網站。

幫助表示讚賞。

+0

如果你做一個dd($ movieshowtime),你是否返回集合? – 2016-09-22 03:21:25

回答

0

此代碼工作完美的我

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


} 

任何建議,以改善這個代碼可以理解的。

0

您可以在模型上使用replicate(),但不能在集合上使用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

+0

我想複製集合,所以複製不會在它上面工作,那麼編寫完整的代碼會更好,它將從表中複製所有這些行並將其重新插入表中......或者還有其他什麼我錯過了 – dollar

+0

foreach($ collection as $ item){$ newItem = $ item-> replicate()} –

+0

@dollar Rolf建議你可以這樣做。事實上,有幾種方法可以做到這一點,只要確保在複製後保存即可! – James

相關問題