Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kikismedia's avatar

Collection::replicate does not exist.

i am trying to clone a movie on MovieShow component im getting this error when i click on the clone button any fix for this

Method Illuminate\Database\Eloquent\Collection::replicate does not exist.

<?php

namespace App\Http\Livewire;

use App\Models\Movie;
use Livewire\Component;
use Carbon\Carbon;
class MovieShow extends Component
{
    public $movie;
    public $body;


    public function mount(Movie $movie)
    {
         $this->movie = $movie;

    }

    public function clone(Movie $id)
    {
        $movie = Movie::find($id);
        $cloneMovie = $movie->replicate();
        $cloneMovie->cloned_movie_id = $movie->id;
        $cloneMovie->updated_at = Carbon::now();
        $cloneMovie->created_at = Carbon::now();
        $cloneMovie->save();
    }
    public function render(Movie $movie)
    {
        $movie->viewcounts();
        return view('livewire.movie-show');
    }
}
0 likes
9 replies
webrobert's avatar
Level 51

@kikismedia,

You're passing a model to find() not an id.

You don't even need the find. Just change $id to movie

    public function clone(Movie $movie)
    {
        $cloneMovie = $movie->replicate();
		...

2 likes
kevinbui's avatar

You got that error because the $movie is an instance of Illuminate\Database\Eloquent\Collection class, which doesn't have the replicate method.

the $id parameter is actually a Movie model, which implements the Arrayable interface, so when you write this:

$movie = Movie::find($id);

The framework interprets that you want to get a collection of movies, and $movie is a collection.

Pls simply do this to avoid confusion, I don't think you have to specify the updated_at and created_at fields either:

public function clone(Movie $movie)
{
    $cloneMovie = $movie->replicate();
    $cloneMovie->cloned_movie_id = $movie->id;
    $cloneMovie->save();
}

Or to make it a bit cleaner:

public function clone(Movie $movie)
{
    $movie->replicate()
	    ->fill(['cloned_movie_id' => $movie->id])
        ->save();
}
1 like
Kikismedia's avatar

@kevinbui @webrobert after following the instructions I'm getting I clicked on the clone button

<button wire:click="clone({{ $movie->Id }})">clone </button>

error

 user_id doesn't have a default value
Snapey's avatar

user_id is probably not fillable

1 like
Kikismedia's avatar

@snapey @kevinbui @webrobert my movie_show livewire blade

<div>
    <div class="">
        <div class="p-1 bg-white rounded-md">
            <div class="flex justify-between p-2 mb-2 text-white bg-gray-400 rounded-md">
                <h3 class="font-bold text-white text-md">{{ $movie->title }}</h3>
                <span class="text-xs">100 views</span>
            </div>

            <img src="{{ asset('/assets/images/blog/img-1.jpg') }}" alt="" class="w-auto rounded-md">
            <p class="p-2">{{ $movie->body }}</p>
         <button wire:click="clone({{ $movie->id }})">clone</button>
        </div>

    </div>
</div>

movie model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;


class Movie extends Model implements Viewable
{
    use HasFactory;
    use InteractsWithViews;

    protected $guarded = [];
    protected $fillable = [
        'title',
        'user_id',
        'body',
        'slug',
        'movie_number',
        'user_id',
        'catalog_id',
        'image',
        'cloned_movie_id',
        'view_count',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function catalog()
    {
        return $this->belongsTo(Catalog::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

     public function viewcounts()
     {
           $this->increment('reads');
     }

     public function clonedMovie()
     {
         return $this->belongsTo(Movie::class, 'cloned_movie_id');
     }
}


Snapey's avatar

you already marked it solved

1 like
Kikismedia's avatar

@Snapey it's not snappy still getting error on the user_id clone is working for inside a foreach loop but not working on single blade

Please or to participate in this conversation.