Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Localization 将变量传递到全局范围,并确定查询输出是否为null Laravel_Localization_Eloquent_Laravel 5_Query Builder_Global Scope - Fatal编程技术网

Localization 将变量传递到全局范围,并确定查询输出是否为null Laravel

Localization 将变量传递到全局范围,并确定查询输出是否为null Laravel,localization,eloquent,laravel-5,query-builder,global-scope,Localization,Eloquent,Laravel 5,Query Builder,Global Scope,我有翻译模型,我想运行全局查询范围,确定当前语言环境并返回相应的值,如果数据库中不存在翻译,则返回到英语 我已经为此创建了一个全局作用域,它运行良好,没有返回英语的能力,因此一些页面崩溃,因为我试图获取NULL属性,并尝试传递一些值,但在构建器中,我无法确定查询是否将返回NULL 如何才能在拉威尔实现这样的目标 我的代码如下: trait WhereLanguage { /** * Boot the Where Language trait for a model.

我有翻译模型,我想运行全局查询范围,确定当前语言环境并返回相应的值,如果数据库中不存在翻译,则返回到英语

我已经为此创建了一个全局作用域,它运行良好,没有返回英语的能力,因此一些页面崩溃,因为我试图获取NULL属性,并尝试传递一些值,但在构建器中,我无法确定查询是否将返回NULL

如何才能在拉威尔实现这样的目标

我的代码如下:

trait WhereLanguage {

    /**
     * Boot the Where Language trait for a model.
     *
     * @return void
     */
   public static function bootWhereLanguage()
    {
        static::addGlobalScope(new WhereLanguageScope);
    }
}
和范围文件:

class WhereLanguageScope implements ScopeInterface {


/**
 * Apply the scope to a given Eloquent query builder.
 *
 * @param  \Illuminate\Database\Eloquent\Builder $builder
 * @param  \Illuminate\Database\Eloquent\Model $model
 */
public function apply(Builder $builder, Model $model)
{
    $this->addWhereLang($builder);
}

/**
 * Remove the scope from the given Eloquent query builder.
 *
 * @param  \Illuminate\Database\Eloquent\Builder $builder
 * @param  \Illuminate\Database\Eloquent\Model $model
 *
 * @return void
 */
public function remove(Builder $builder, Model $model)
{
    $query = $builder->getQuery();
    foreach ((array) $query->wheres as $key => $where)
    {
        // If the where clause is a soft delete date constraint, we will remove it from
        // the query and reset the keys on the wheres. This allows this developer to
        // include deleted model in a relationship result set that is lazy loaded.
        if ($where['column'] == 'lang_id')
        {
            unset($query->wheres[$key]);
            $query->wheres = array_values($query->wheres);
        }
    }
}


/**
 * Extend Builder with custom method.
 *
 * @param \Illuminate\Database\Eloquent\Builder $builder
 *
 */
protected function addWhereLang(Builder $builder)
{
    $builder->macro('whereLang', function(Builder $builder)
    {

        // here 1 is ID for English,
        // 48 Arabic, 17 Netherlands...etc 
        // and It was the App:currentlocale() passed into Language model to determine the ID of current locale.
        // but for testing now I hard coded it with ID of 48
        $builder->where('lang_id','=','48');  
        return $builder;
    });
}
}

用法示例:

$title = $centre->translations()->whereLang()->first()->name;
其中center是我的模型,没有本地化,translation是处理center和centretransation之间关系的方法的名称


顺便说一句,我不想强制传递变量。

而且我肯定在模型中使用了我的trait
使用WhereLanguage