Support the ongoing development of Laravel.io →
Database Eloquent

Hello there,

I have a web app that manages gardening courses and registrations for it. While entering new courses I came up with the idea to create a controller action to duplicate an existing course using its course_id.

unfortunately this does not seem to work as the method replicate() which I found in the documentation doesnt seem to be defined. I guess it actually is, but wheres my misunderstanding?

my controller function :

public function getDuplicate($course_id = false) {

		if (!$course_id) {

			Notification::info('Es wurde kein Kurs angegeben.');
			return Redirect::route('home');

		} else {

			$oldCourse = Course::with('author')->where('course_id','=',$course_id)->get();
			$newCourse = $oldCourse->replicate();
			unset($newCourse->course_id);

			if($newCourse->save()) {

        		Notification::success('Der Kurs wurde dupliziert. ');
				return Redirect::action('CourseController@getEdit', array('id' => $newCourse->id));

			} else {

				Notification::info('Der Kurs konnte nicht dupliziert werden..');
				return Redirect::route('home');

			}

		}
	} 

As you can see, I am not a Laravel master and therefore would be grateful to get some help - thanks!

Last updated 2 years ago.
0

Hello MisterMike,
I never used replicate, but it seems to me that you have an error.

$oldCourse = Course::with('author')->where('course_id','=',$course_id)->get();

will return a collection of courses, not a signle course. Use this instead.

$oldCourse = Course::with('author')->where('course_id','=',$course_id)->first();

Hope it helps. Gern geschehen!

0

Yes my same conclusion. The replicate method is found on Eloquent Models not Collections

That is your issue.

0

Firtzberg said:

Firtzberg added well.

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

MisterMike mistermike Joined 20 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.