简体   繁体   English

将数组传递给雄辩模型的静态方法

[英]Pass an Array to Static Method of Eloquent Model

I'm using Laravel and I have a need to pass an associative array to a static function in an eloquent model. 我正在使用Laravel,我需要在雄辩的模型中将关联数组传递给静态函数。

class MyClass extends Eloquent\Model
{
    static function scopeRegister($input) {
        return $input['key1'];
    }
}

Running with Tinker, here is my input: 与Tinker一起运行,这是我的输入:

$input = array('key1'=>'value', 'key2'=>'value', 'key3'=> 'value', 'key4'=>'value');

When I try it, though, I'm getting an error: 但是,当我尝试时,出现错误:

PHP error: Cannot use object of type Illuminate\\Database\\Eloquent\\Builder as array PHP错误:无法将类型为Illuminate \\ Database \\ Eloquent \\ Builder的对象用作数组

And when I type-hint array: 当我键入提示数组时:

class MyClass extends Eloquent\Model
{
    static function scopeRegister(array $input) {
        return $input['key1'];
    }
}

I get the following error: 我收到以下错误:

Argument 1 passed to App\\KeywordGeotarget::scopeRegister() must be of the type array, object given 传递给App \\ KeywordGeotarget :: scopeRegister()的参数1必须为数组类型,给定对象

What's happening here and how can I access the values defined in the array from the static method within the model? 这是怎么回事,如何从模型中的静态方法访问数组中定义的值?


EDIT: I'm just using tinker right now to test this but here is the code I'm using to call the method: 编辑:我现在只是使用修补程序来对此进行测试,但这是我用来调用该方法的代码:

php artisan tinker
>> $input = array('key1'=>'value','key2'=>'value','key3'=>'value', 'key4'=>'value');
>> App\MyClass::register($input);

Since it's a scope, you need to add a $query as the first parameter and then do something like this: 由于这是一个作用域,因此您需要添加一个$query作为第一个参数,然后执行以下操作:

static function scopeRegister($query, array $input)
{
    return $query->where($input['key1']);
}

If you just want to have a method and you don't want to use it as a local scope, just rename the method (remove scope part): 如果您只想拥有一个方法而又不想将其用作本地范围,则只需重命名该方法(删除scope部分)即可:

static function register(array $input)

Your understanding for the adding query scope is wrong I guess you need to go through documentation I think. 您对增加查询范围的理解是错误的,我想您需要阅读我认为的文档。 Check it over here. 在这里检查。 The scopes represents the query builder properties and you can't get builder class properties as array. 范围代表查询构建器属性,您不能将构建器类属性作为数组获取。 You should use it like this. 您应该这样使用它。

class MyClass extends Eloquent\Model
{
    public function scopeRegister($query) {

        return $query->where(// perform your where here);

    }
}

You are defining an eloquent scope. 您正在定义一个雄辩的范围。 Scopes do not work with arrays, but with queries. 范围不适用于数组,但适用于查询。

You can read more about how to declare and use scopes here https://laravel.com/docs/5.5/eloquent#local-scopes 您可以在这里阅读有关如何声明和使用范围的更多信息https://laravel.com/docs/5.5/eloquent#local-scopes

If you want to call register() like you did in the tinker example, do this instead (just remove the scope): 如果要像在修补程序示例中那样调用register() ,请改为执行此操作(只需删除作用域):

class MyClass extends Eloquent\Model
{
    static function register(array $input) {
        return $input['key1'];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel雄辩的模型重写静态启动方法 - Laravel Eloquent model overriding static boot method Laravel5:在雄辩的模型上调用静态方法不起作用 - Laravel5 : Calling a static method on eloquent model not working 使用Eloquent Model类。 静态方法“where”的实现在哪里? - Using Eloquent Model class. Where is the implementation of static method “where”? 我们可以通过一个模型来更新雄辩的Laravel的updateOrCreate方法吗? - Can we pass a model to updateOrCreate method of eloquent Laravel? 通过雄辩的方法作为参数 - Pass eloquent method as a parameters 将雄辩的集合传递给数组 - Pass eloquent collection to array Cannot make non static method Illuminate\Database\Eloquent\Model::getTable() static in class App\bill, Laravel - Cannot make non static method Illuminate\Database\Eloquent\Model::getTable() static in class App\bill, Laravel 为什么在调用 Eloquent 模型中的方法时出现“不应静态调用非静态方法”? - Why I'm getting 'Non-static method should not be called statically' when invoking a method in a Eloquent model? 不应静态调用非静态方法 Illuminate\Database\Eloquent\Model::update() - Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically 雄辩模型的创建方法在哪里? - Where is the create method for Eloquent Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM