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!
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!
Yes my same conclusion. The replicate method is found on Eloquent Models not Collections
That is your issue.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community