【问题标题】:Laravel-Global scope trait not being hitLaravel-全局范围特征没有被击中
【发布时间】:2015-01-31 16:47:53
【问题描述】:

我似乎无法在这里找到问题。我正在使用 trait 将全局范围附加到模型上的所有 Eloquent 查询。这是我的模型

<?php namespace App;

 use Illuminate\Database\Eloquent\Model;
 use App\Club\traits\restrictToClubTrait;


class Product extends Model
{



public function category()
{
    return $this->belongsTo('App\ProductCategory', 'product_category_id', 'id');
}


public function producer()
{
    return $this->belongsTo('App\Producer', 'producer_id');
}
}

这是特点

<?php namespace App\Club\traits;

trait restrictToClubTrait
{

/**
 * Boot the soft deleting trait for a model.
 *
 * @return void
 */
public static function bootRestrictToClubTrait()
{
    dd('p');
    static::addGlobalScope(new RestrictToClubScope);
}

}

那个 dd 永远不会被击中,所以函数一定不会被击中,我已经倒在了文档上,但我看不出我哪里出错了。

【问题讨论】:

    标签: php laravel eloquent laravel-5


    【解决方案1】:

    特征应该“包含”在类体内。欲了解更多信息here

    use App\Club\traits\restrictToClubTrait;
    
    class Product extends Model {
        use restrictToClubTrait;
    }
    

    【讨论】:

    • 该死,我知道这会很简单。谢谢您,先生,我会尽快标记正确。
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 2016-11-11
    • 2016-04-14
    • 2015-12-02
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多