This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

hambern
hambern

Hi there. Could someone just point me in the right direction. I'm developing a teacher-plugin for October CMS based of Courses. Every course has a number of objectives and a number of assignments, and every assignment has a couple of objectives as well. I have the tables: Courses, Assignments and Objectives and a pivot table: Assignments_Objectives. Everything is working fine except for one thing. When I am to pick objectives to a specific assignment (opened from the course as a modal) i want the list to show ONLY the objectives that belongs to the same course as the assignment. Get it?

When i write:

objectives:
        tab: Objectives
        label: Objectives
        type: relation
        prompt: Add at least one objective
        nameFrom: title

I of course get ALL objectives from ALL courses. But I only want to pick from the objectives that belongs to the active course. The course in the background of the modal-window. Is this possible?

In the Assignment model i have this:

public $belongsTo = [
    'course' => ['Hambern\Teacher\Models\Course']
];
public $belongsToMany = [
    'objectives' => [
        'Hambern\Teacher\Models\Objective',
        'table' => 'hambern_teacher_assignments_objectives'
    ]
];

Please just point me in the right direction here

Last updated

hambern
hambern

What I need is something like this:

objectives:
        tab: Objectives
        label: Objectives
        type: relation
        prompt: Add at least one objective
        nameFrom: title
        options: $this->course->objectives
chris10207
chris10207

Hi,

i have the same requirement with a different model, any solution on your side ?

thanks

hambern
hambern

you have to create à getFooOptions() in your model and use that to filter.

herby
herby

Would you mind sharing an example of your solution?

chris10207
chris10207

in the yaml file, if you are using a method to retrieve the optins for the dropdown, then you dont need to specify the key 'options', but just the key 'type' then in your case, in your Course class model you have to create a method like this

   
 public function getObjectivesOptions()
    {
        return Objectives::lists('name', 'id'); // your code to return the list
    }
herby
herby

Hi Chris, thank you for your helpful answer. Just to to complete the answer:

The get*Foo*Options() method must be declared inside the Model that relates to the *.yaml file. The type must be declared regarding the form fields definition (Dropdown, Radio List, or Checkbox List). The type:relation is not allowed in this context.

douglas14166
douglas14166

I had a similar issue. I needed to limit the scope to only show variants that belonged to a particular class (via related 'product', which is related to a 'category', which is related to 'class'). In this case, the class 'slug' is passed to the scope method.


public $belongsToMany = [
        'compatible_accessories' => [
            'Pina\Products\Models\Variant',
            'table' => 'pina_products_variants_accessories',
            'key' => 'variant_id',
            'otherKey' => 'accessory_id',
            'scope' => 'accessories'
        ],
    ];

/**
     * Scope query to only return variants related to product classes 'accessories' or 'covers-and-jackets'
     * Used to make the compatible accessories list more manageable
     */
    public function scopeAccessories($query)
    {
        return $this->scopeOfClass($query, ['accessories','covers-and-jackets']);
    }

/**
     * Scope query to only return variants related to the specified product class(es) via product, category
     * 
     * @var $query The current query
     * @var $class Mixed A product class slug or an array of product class slugs.
     */
    public function scopeOfClass($query, $class)
    {
        if (!is_array($class)) {
            $class = array($class);
        }

        return $query->whereHas('product', function ($q) use ($class)
        {
            $q->whereHas('category', function ($q) use ($class) 
            {
                $q->whereHas('class', function($q) use ($class) 
                {
                    $q->whereIn('slug', $class);
                });
            });
        });
    }

Last updated

1-8 of 8

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.