我試圖返回一個對象Contract
及其全部相關Project
。我可以退回所有的Contract
s,但是當我嘗試獲得合同的Project
時,我收到「Class'EstimateProject'not found」錯誤。我運行了composer dump-autoload
來重新加載類映射,但我仍然收到錯誤。有任何想法嗎?這是我的班級設置:找不到一對多的Laravel類
編輯:只是想補充一點,LaravelBook\Ardent\Ardent\
是Laravel的Model.php的擴展。它爲Save
函數的模型添加了驗證。我已經讓Ardent擴展了我添加的另一個插件,它是一個MongoDB版本的Eloquent ORM。
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;
}
}
}
}
嘗試在'EstimateContractController.php中放入'use \ Test \ Tools \ EstimateProject;'' –
@TryingTobemyself不幸的是,我已經試過了,無濟於事。 – Thelonias
嘗試在'EstimateContract.php'中放入'use \ Test \ Tools \ EstimateProject;' –