0

In my Laravel task scheduler, I am trying to make an object of a model and call a function of that model. Inside the function I am trying to access a model property using $this keyword. It throws an exception indicating that the property is not defined. Please note that the same code is working perfectly in a normal controller and the exception occurs only when I run it by task scheduler.

Here is a simplified version of my code in kernel.php

$schedule->call(function () {
   $group_set_id = 8345;       
   $group_set = new GroupCategory(['group_set_id' => $group_set_id]);
   $group_set->changeSelfSignup(true);                
}

Here is what I have in model:

class GroupCategory extends Model
{
protected $fillable = [
    'id', 'group_set_id', 'course_ids', 'course_names', 'section_ids', 'section_names', 'auto_update'
];

protected $attributes = [
    'id',
    'group_set_id' => '',
    'name' => '',
    'role' => '',
    'self_signup' => null,
    'auto_leader' => null,
    'context_type' => '',
    'account_id' => '',
    'group_limit' => null,
    'sis_group_category_id' => null,
    'sis_import_id' => null,
    'progress' => null
];

protected $primaryKey = 'id';

public $timestamps = true;

public function getGroupCategoryGroups()
{
    $type = 'get';
    $form_params = ['include' => 'email'];
    $url = APIUtility::getGroupCategoryGroupsURL($this->group_set_id) . '?per_page=100';
    return APIUtility::getResponse($type, $url, $form_params);
}

public function createGroup(string $group_name)
{
    $type = 'post';
    $form_params = ['name' => $group_name];
    $url = APIUtility::createGroupURL($this->group_set_id);
    return APIUtility::getResponse($type, $url, $form_params);
}

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
}

protected $primaryKey = 'id';

public function changeSelfSignup(bool $is_self_signup_allowed)
{
    $type = 'put';
    $form_params = ['self_signup' => $is_self_signup_allowed ? 'enabled' : 'disabled'];
    $url = APIUtility::getSelfSignupURL($this->group_set_id);
    return APIUtility::getResponse($type, $url, $form_params);
}

Here is the exception I am getting:

ErrorException: Undefined variable: group_set_id in /var/www/utagt/app/GroupCategory.php:78

Any idea would be appreciated.

  • can we see line 78 in group category where the error is? – mrhn Feb 15 '19 at 14:59
  • That's exactly this line: $url = APIUtility::getSelfSignupURL($this->group_set_id); –  Feb 15 '19 at 15:02
  • 2
    share full GroupCategory class – the_hasanov Feb 15 '19 at 15:29
  • @the_hasanov I just edited the question and added the entire class. –  Feb 15 '19 at 16:29
  • This is super weird to me. Can you please generate a call stack just to ensure it's being called from where you think it is? https://stackoverflow.com/a/7039409/823549 – 1000Nettles Feb 15 '19 at 16:32
  • I am writing the exception into a specific table in my database. From there I can see the stack trace, and I am sure about where the exception is being thrown. –  Feb 15 '19 at 16:39

1 Answers1

0

I made a change in my code which made it work, but I still don't know why I cannot access the property. Here is how I changed my code:

In GroupCategory model:

public function changeSelfSignup(bool $is_self_signup_allowed, $group_set_id = '')
{
    $group_set_id = $group_set_id === '' ? $this->$group_set_id : $group_set_id;
    $type = 'put';
    $form_params = ['self_signup' => $is_self_signup_allowed ? 'enabled' : 'disabled'];
    $url = APIUtility::getSelfSignupURL($group_set_id);
    return APIUtility::getResponse($type, $url, $form_params);
}

And in my task scheduler:

$schedule->call(function () {
   $group_set_id = 8345;       
   $group_set = new GroupCategory(['group_set_id' => $group_set_id]);
   $group_set->changeSelfSignup(true, $group_set_id);                
}