Laravel从静态函数调用函数

时间:2016-07-05 15:47:12

标签: php laravel

我有一个客户模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
protected $fillable = ['name'];

public function tool()
{
    return $this->belongsToMany('App\Tool');
}

public function user()
{
    return $this->hasMany('App\User');
}

public function createSetting($name, $slug, $classname, $attribute, $value, $customer_id, $priority){
    \App\Setting::create([
        'name' => $name,
        'slug' => $slug,
        'classname' => $classname,
        'attribute' => $attribute,
        'value' => $value,
        'customer_id' => $customer_id,
        'priority' => $priority,
    ]);
}

public static function boot()
{
    parent::boot();

    static::created(function($customer)
    {
        $customer_id = $customer->id;
        createSetting('text_primary', 'Primaire tekst kleur', 'dml-pr-text', 'color', 'FFFFFF', $customer_id, 1);
        createSetting('text_primary', 'Primaire tekst kleur', 'dml-pr-text', 'color', 'FFFFFF', $customer_id, 1);
        createSetting('text_primary', 'Primaire tekst kleur', 'dml-pr-text', 'color', 'FFFFFF', $customer_id, 1);
        createSetting('text_primary', 'Primaire tekst kleur', 'dml-pr-text', 'color', 'FFFFFF', $customer_id, 1);
    });

}
}

创建新客户后,将立即调用静态创建函数,尝试将一些设置添加到数据库中。但是模型中的createSetting方法不在静态函数的范围内。所以我收到了错误

Call to undefined function App\createSetting()

如何确保可以从静态函数到达createSetting方法?

0 个答案:

没有答案