Laravel 非静态方法问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14126651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:27:53  来源:igfitidea点击:

Laravel Non-static method issue

phpmodellaravelnon-static

提问by Alex

Having the following Models:

有以下型号:

news.php

新闻.php

class News extends Aware {

    public static $table = 'noticia';
    public static $key = 'idnoticia';
    public static $timestamps = false;

    public static $rules = array(
        'titulo' => 'required',
        'subtitulo' => 'required',
    );

    public function images()
    {
        return $this->has_many('Image');
    }
}

image.php

图片.php

class Image extends Aware {

    public static $timestamps = true;

    public static $rules = array(
        'unique_name' => 'required',
        'original_name' => 'required',
        'location' => 'required',
        'news_id' => 'required',
    );

    public function news()
    {
        return $this->belongs_to('News');
    }

}

Then in a controller I do the following:

然后在控制器中我执行以下操作:

$image = new Image(array(
    'unique_name' => $fileName,
    'original_name' => $file['file']['name'],
    'location' => $directory.$fileName,
    'news_id' => $news_id,
));
News::images()->insert($image);

I keep getting the following error message:

我不断收到以下错误消息:

Non-static method News::images() should not be called statically, assuming $this from incompatible context

非静态方法 News::images() 不应静态调用,假设 $this 来自不兼容的上下文

Any ideas what am I doing wrong?

任何想法我做错了什么?

Setting public static function images()doesn't seem to be wanted, as after a refresh I get an error saying

public static function images()似乎不需要设置,因为刷新后我收到一条错误消息

$this when not in object context

$this 不在对象上下文中时

Gordon said that by doing News::images()->insert($image);I'm doing a static call, but that's how saw to do it

戈登说,通过这样做,News::images()->insert($image);我正在做一个静态调用,但这就是看到的方法

回答by Halcyon

You're using $thisin a function that is called statically. That's not possible.

您正在$this一个名为statically的函数中使用。那是不可能的。

$thisbecomes available only after you create an instance with new.

$this仅在您使用new.

If you turn on strict mode you will get another error, namely that imagesis not a static function and thus shouldn't be called statically.

如果您打开严格模式,您将收到另一个错误,即它images不是静态函数,因此不应静态调用。

The problem is in News::images(), not in images()->insert($image);

问题出在News::images(),不在images()->insert($image);

回答by Nicholas Ruunu

You are missing some steps.

你错过了一些步骤。

The Image belongs to News, but you're not referencing the News post you want to update.
You probably want to do:

图像属于新闻,但您没有引用要更新的新闻帖子。
你可能想做:

$image = new Image(array(...));
$news = News::find($news_id);
$news->images()->insert($image);

More in the docs.

文档中的更多内容。

回答by Martin Samson

$this can only be used within an object instance. Class::method() calls a static method of the specified class.

$this 只能在对象实例中使用。Class::method() 调用指定类的静态方法。

In your case, you mixed both.

在你的情况下,你混合了两者。

Your function definition for images is for an object instance:

您的图像函数定义适用于对象实例:

public function images()
{
    return $this->has_many('Image');
}

You are calling it as a static method:

您将其称为静态方法:

News::images()->insert($image);

The Newsclass would need to be instantiated or the images method be modified to support static calls.

News班将需要实例化或图像的方法进行修改,以支持静态调用。