9

我正在尝试返回一个对象Contract并且所有它都是相关的Project。我可以退回所有的Contracts,但是当我尝试获取合同时Project,我收到“找不到类 'EstimateProject'”错误。我已经运行composer dump-autoload重新加载类映射,但我仍然得到错误。有任何想法吗?这是我的班级设置:

编辑:只是想添加它LaravelBook\Ardent\Ardent\是 Laravel 的 Model.php 的扩展。Save它为函数的模型添加了验证。我已经让 Ardent 扩展了我添加的另一个插件,它是 Eloquent ORM 的 MongoDB 版本。

EstimateContract.php

<?php namespace Test\Tools;

  use LaravelBook\Ardent\Ardent;

  class EstimateContract extends Ardent {

     // This sets the value on the Mongodb plugin's '$collection'
     protected $collection = 'Contracts';

     public function projects()
     {
        return $this->hasMany('EstimateProject', 'contractId');
     }
  }

估计项目.php

<?php namespace Test\Tools;

  use LaravelBook\Ardent\Ardent;

  class EstimateProject extends Ardent {

   // This sets the value on the Mongodb plugin's '$collection'
   protected $collection = 'Projects';

   public function contract()
   {
      return $this->belongsTo('EstimateContract', 'contractId');
   }
}

EstimateContractController.php

<?php

  use  \Test\Tools\EstimateContract;

  class EstimateContractsController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
    public function index()
    {
        $contracts = EstimateContract::all();

        echo $contracts;

        foreach($contracts as $contract)
        {
            if($contract->projects)
            {
                echo $contract->projects;
            }
        }
     }
}
4

2 回答 2

25

为了使它工作,我需要在我的 EstimateContract 模型中完全限定 EstimateProject 字符串。

解决方案是将其更改为:

return $this->hasMany('EstimateProject', 'contractId'); 

return $this->hasMany('\Test\Tools\EstimateProject', 'contractId');
于 2013-08-06T17:36:35.930 回答
2

You have to use the fully qualified name, but I got the same error when I used forward slashes instead of back slashes:

//Correct
return $this->hasMany('Fully\Qualified\ClassName');
//Incorrect
return $this->hasMany('Fully/Qualified/ClassName');
于 2015-09-10T16:41:34.610 回答