2013-08-06 23 views
9

Tôi đang cố trả lại một đối tượng Contract và tất cả các đối tượng liên quan là Project. Tôi có thể trả lại tất cả các số Contract s nhưng khi tôi cố gắng để có được hợp đồng của Project, tôi nhận được lỗi "Lớp 'EstimateProject' không tìm thấy". Tôi đã chạy composer dump-autoload để tải lại ánh xạ lớp, nhưng tôi vẫn gặp lỗi. Bất kỳ ý tưởng? Đây là thiết lập lớp học của tôi:Không tìm thấy lớp Laravel với một-nhiều-nhiều

EDIT: Chỉ muốn thêm rằng LaravelBook\Ardent\Ardent\ là một phần mở rộng của Model.php Laravel của. Nó thêm xác thực để mô hình hóa trên hàm Save. Tôi đã làm cho Ardent mở rộng một plugin mà tôi đã thêm vào đó là một phiên bản MongoDB của ORlo Eloquent.

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; 
      } 
     } 
    } 
} 
+2

thử đặt 'sử dụng \ Test \ Tools \ EstimateProject;' trong 'EstimateContractController.php' –

+0

@TryingTobemyself không may, tôi đã cố gắng mà không có kết quả. – Thelonias

+1

thử đặt 'sử dụng \ Test \ Tools \ EstimateProject;' trong 'EstimateContract.php' –

Trả lời

24

Để làm việc này, tôi cần phải đủ điều kiện chuỗi EstimateProject trong mô hình EstimateContract của tôi.

Giải pháp là thay đổi nó từ:

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

để

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

Bạn phải sử dụng tên đầy đủ, nhưng tôi đã nhận lỗi tương tự khi tôi sử dụng dấu gạch chéo thay vì trở lại dấu gạch chéo:

//Correct 
return $this->hasMany('Fully\Qualified\ClassName'); 
//Incorrect 
return $this->hasMany('Fully/Qualified/ClassName');