Laravel 4:保存多态关系的记录:未定义的方法newQuery() [英] Laravel 4: save a polymorphic relationship's record: undefined method newQuery()

查看:362
本文介绍了Laravel 4:保存多态关系的记录:未定义的方法newQuery()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 4中具有多态关系,并且可以按我想要的方式工作.我有另一个模型,即使我尝试插入相关模型,也无法使用,即使它与第一个模型相同,并且我也以相同的方式使用它.

I have a polymorphic relationship in Laravel 4 and it's working like I want. I have another one, which doesn't work when I try to insert the related model, even though it is identical to the first one and I use it in the same way.

我有Event模型和Article模型:

// Article Model
class Article extends Eloquent {
    public function events() {
        return $this->morphMany('Event', 'eventable');
    }
    ...

// Event Model
class Event extends Eloquent {
    public function eventable() {
        return $this->morphTo();
    }
}

用法:

$article = Article::find($article_id);
// insert
// neither of the following lines work
$article->events()->create(array("type" => "click"));
$article->events()->save(new Event(array("type" => "click")));

我遇到相同的错误:

Error: Call to undefined method Illuminate\Support\Facades\Event::newQuery() in
\app_path\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php line 383

我做错了什么?我对文章"有一个相同的情况,它是照片",但是在这里有效(这也是一种多态关系).

What am I doing wrong? I have an identical situation with an Article and it's Photos, but there it works (it's also a polymorphic relation).

推荐答案

事件可能是Laravel中的保留字.除非您为它命名空间,否则建议您避免创建一个名为Event的类,因为它类似于Laravel核心中使用的类.诸如活动之类的东西可能可以替代.

Event is likely a reserved word in Laravel. Unless you namespace it, i'd suggest avoiding creating a class called Event, since it is like used in Laravel core. Something like activity could potentially serve as an alternative.

这篇关于Laravel 4:保存多态关系的记录:未定义的方法newQuery()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆