I've created an anonymous function inside my method, it is just returning an item if the deleted field is equal to 0. But the problem is it is still returning an item even if the item's deleted field in the database is equal to 1. Please see my code below: Note: This is working with topics only but not the rest.
$undeleted = function($query){
return $query->where('deleted', 0);
};
$course = Course::with([
'assignments' => $undeleted,
'quizzes' => $undeleted,
'add_links' => $undeleted,
'topics' => $undeleted
])->findOrFail($id);
$course_items = collect($course->assignments);
$course_items = $course_items->merge(collect($course->quizzes));
$course_items = $course_items->merge(collect($course->add_links));
$course_items = $course_items->sortBy('item_number');
Update:
Schema
Schema::create('topics', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();
$table->text('body');
$table->tinyInteger('deleted');
$table->tinyInteger('hidden');
$table->timestamps();
});
Relationships
Course model:
I only used belongsToMany with assignments, quizzes, add_links, and topics and just adding withPivot(all the fields) and withTimestamps. I've also added the deleted in the fillable attribute.