【问题标题】:Replicate Laravel collection - Laravel 5.3复制 Laravel 集合 - Laravel 5.3
【发布时间】:2017-03-17 04:55:01
【问题描述】:

我正在复制行程表。在旅行表格上,有一个选择物种的下拉菜单。一个用户可以选择多个物种。我无法复制这个物种表,因为它在它自己的表中,并且它是一个集合。所以使用“复制”是行不通的。

这就是我现在复制行程表的方式:

public function replicateTrip (Request $request, $slug, $id) {

        $listing = $request->user()->listings()->where('slug', $slug)->first();
        $trip = $listing->trips()->where('id', $id)->first();

        $replicateTrip = Trip::find($trip->id);
        // This is how im getting the species from the species table
        $replicateSpecies = DB::table('species_trip')->where('trip_id', $id)->get();

        $newTask = $replicateTrip->replicate();

        $newTask->save();

        return redirect()->back();

}

如果我在克隆当前行程时 DD $replicateSpecies 变量,我会得到:

我需要将原始旅行中的物种数组复制到物种表中,我不能只使用“复制”,因为它是一个集合。

所以我的问题是如何复制这个集合?或者如果有其他方法可以做到这一点?

【问题讨论】:

    标签: laravel collections replication laravel-5.3


    【解决方案1】:

    你可以试试:

    public function replicateTrip (Request $request, $slug, $id) {
    
        $listing = $request->user()->listings()->where('slug', $slug)->first();
        $trip = $listing->trips()->where('id', $id)->first();
    
        $replicateTrip = Trip::find($trip->id);
    
        // This is how im getting the species from the species table
        $replicateSpecies = DB::table('species_trip')->where('trip_id', $id)->get();
    
        $newTask = $replicateTrip->replicate();
    
        $newTask->save();
    
        $replicateSpecies->each(function ($item, $key) use($newTask) {
            $copy = $item->replicate();
            $copy->trip_id = $newTask->id;
            $copy->save();
        })
    
        return redirect()->back();
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2017-06-04
      • 2017-08-05
      • 1970-01-01
      • 2018-07-20
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多