【问题标题】:Error accessing relational data using eloquent使用 eloquent 访问关系数据时出错
【发布时间】:2014-05-13 14:21:17
【问题描述】:

我正在尝试这样做

$this->current_user->company->routes

我希望根据当前登录的用户公司获得路线列表。

这是我得到的错误

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method Illuminate\Support\Facades\Route::newQuery()

我知道我从以下两个方面都得到了正确的信息

$this->current_user

$this->current_user->company

这是我的公司模型

<?php

class Company extends Eloquent
{
protected $guarded = array(
    'id',
    'created_at',
    'updated_at',
);

public static function getDropDownArray()
{
    $return = array();
    $companies = Company::all();

    foreach($companies as &$company)
    {
        $return[$company->id] = $company->title;
    }

    return $return;
}

public function routes()
{
    return $this->hasMany('Route');
}

public function users()
{
    return $this->hasMany('User');
}

public function locations()
{
    return $this->hasMany('Location');
}

public function publications()
{
    return $this->hasMany('Publication');
}

public function racks()
{
    return $this->hasMany('Rack');
}
}

【问题讨论】:

    标签: php orm laravel eloquent


    【解决方案1】:

    你必须将你的 Eloquent 类之一命名为 Route,这与 Laravel 的 Route 别名冲突。

    所以你有两个选择:

    1) 使用命名空间

    2) 重命名你的类

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 2017-02-17
      相关资源
      最近更新 更多