Closed connecteev closed 1 year ago
Found a temporary fix for this. sort_when_creating
should be set to false
. If set to true
, the seeding gets affected.
In CourseSection model:
class CourseSection extends Model implements Sortable
{
use HasFactory, SortableTrait;
public array $sortable = [
'order_column_name' => 'section_order',
// sort_when_creating should be false (at least during seeding), otherwise the order_column_name field gets messed up in the database during seeding
'sort_when_creating' => false,
'sort_on_has_many' => true,
'nova_order_by' => 'ASC',
];
However, if we need sort_when_creating
to be set to true, then the following approach could work during seeding. We have to reset the section_order for each section within a course:
In CourseContentSeeder:
// Create CourseSections with default sorting
$courses->each(function ($course) {
CourseSection::factory()
->count(4)
->withSlug()
->create([
'course_id' => $course->id,
]);
// If sort_when_creating (under the model's sortable property) is not false during seeding, the order_column_name field (mapped to section_order) gets messed up in the database during seeding
// We have to reset the section_order for each section within a course
$course->courseSections->each(function ($section, $index) {
$section->update([
'section_order' => $index + 1,
]);
});
});
Thanks for this package, because I really need a way to sort resource rows in Nova using drag-and-drop. However, here is an issue that I can't seem to fix.
Problem
This package overrides the "sort order" database field (specified by
order_column_name
) when seeding the database. As a result, I cannot seed the database table correctly.My goal is simple:
To allow CourseSections to be sortable using drag-and-drop on the Course Detail page in Nova.
My Model relationships:
Here is my code for the models and Nova resources:
Database Table, Factory, Seeder etc:
Output when I run CourseContentSeeder:
As you can see, the seeder tries to create the section with the correct section_order (in the $sectionNum variable), and what I want is for the section_order database field to be 1, 2, 3, 4 for course 1, and then 1, 2, 3 4 again for course 2, and so on.
However, what I see in reality is that this package is overriding the section_order database field when the course_sections table is being seeded. The section orders do not reset from course 1 to course 2, and go from 1....8, as you can see below.
Root cause??
I was able to isolate this to the
order_column_name
column in the model. If I changeorder_column_name
to any other value, sayorder
, then the seeding works fine (section_order is 1, 2, 3, 4 for course 1; and section order is 1, 2, 3, 4 for course 2).How do I fix this? Without resolving this, I cannot seed the database correctly.