0
votes

Everything is working fine in this code except $result = Input::get('term'); Someone please help me to get the input value of field term. When i do this $result = 8; everything works well.

Form in plugin backend

In my plugin "Lesson" model

<?php

 namespace Cng\Tennis\Models;

 use Model;
 use Flash;
 use Db;
 use Input;
 use request;
 use Cng\Tennis\Models\Term as termModel;

 class Lesson extends Model
 {  
public function getSdateOptions () {

  $result = Input::get('term');
  $term = termModel::select('id','start_date')->where('id',  $result )->first();

   if ($this->term_id == $result ) { 
        return [$term->id => $term->start_date];   
    }
    else{
      return ['Select a date' => 'Select a date' ];
      }
    }

public function getFdateOptions () {
        return ['Select a date' => 'Select a date' ];
    }

In yaml

    term:
        label: Term
        nameFrom: name
        descriptionFrom: description
        span: auto
        containerAttributes: {  }
        type: dropdown
        emptyOption: Select
        tab: 'Event Details'
        dependsOn:
            - location

    sdate:
        label: 'Start Date'
        mode: date
        span: left1
        cssClass: ''
        required: 1
        dependsOn:
            - term
        type: dropdown
        tab: 'Event Details'
        disabled: 1
2

2 Answers

1
votes

First off, your problem is caused by the fact that the term field is not named that in the HTML. If you look at the generated form inputs in the browser, or in the AJAX request that's sent when the dependsOn trigger is fired, you'll notice that the actual field name for the term field is prefixed with the Form widget's alias (probably Form, so in this case the actual field name is Form[term]).

However, you don't even have to use Input at all, you can just reference the model property the term field refers to directly in your function:

$options = [0 => 'Select a start date'];
$term = TermModel::find($this->term);

if ($term) {
    $options = [$term->id => $term->start_date];
}

return $options;

See https://octobercms.com/docs/backend/forms#field-dependencies for more information on how to properly utilize the dependsOn property. Note that you could probably combine it in your case with the trigger property to also hide the start_date field until you have a valid option selected in the term field.

-1
votes

Try replacing the select('id','start_date') with all() or find($result) and if you only want to grab the start date do pluck->('start_date'). Seems you can't use select on eloquent models which makes sense in my test.

This is what I get when I run select without the DB call:

    $names = Products::select('id','name')->where('id', 1);
    return $names;

enter image description here

However when I access the model by normal collection query methods I get this:

    $names = Products::all()->where('id', 1);
    return $names;

enter image description here

Lastly you can just use $names = Products::find('1')->pluck('name')->first(); replace 1 with $result and 'name' with 'start_date' in your case.