一对多找不到 Laravel 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18084310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:14:19  来源:igfitidea点击:

Laravel class not found with one-to-many

phpmongodblaravellaravel-4

提问by Thelonias

I'm trying to return an object Contractand all of it's related Project. I can return all of the Contracts but when I try to get the contract's Project, I get a "Class 'EstimateProject' not found" error. I've run composer dump-autoloadto reload the class mappings, but I still get the error. Any ideas? Here's my class setup:

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

EDIT:Just wanted to add that LaravelBook\Ardent\Ardent\is an extension of Laravel's Model.php. It adds validation to model on the Savefunction. I've made Ardent extend another plugin I've added that is a MongoDB version of the Eloquent ORM.

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

EstimateContract.php

估计合同.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

<?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

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

回答by Thelonias

In order for this to work, I needed to fully qualify the EstimateProject string in my EstimateContract model.

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

The solution was to change it from:

解决方案是将其更改为:

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

to

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

回答by ajon

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');