没有找到一对多的Laravel课程

时间:2013-08-06 15:24:13

标签: php mongodb laravel laravel-4

我正在尝试返回一个对象Contract,并且所有这些都与Project相关。我可以返回所有Contract但是当我尝试获得合同的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');
     }
  }

EstimateProject.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;
            }
        }
     }
}

1 个答案:

答案 0 :(得分:25)

为了实现这一点,我需要在EstimateContract模型中完全限定EstimateProject字符串。

解决方案是改变它:

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

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