【问题标题】:Laravel : Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\RelationLaravel:关系方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象
【发布时间】:2018-03-20 13:58:36
【问题描述】:

大家好,我需要帮助!!! 所以我有 3 个模型:Machine、Prod_model 和 Group 机器型号:

namespace App;

use Illuminate\Database\Eloquent\Model;


class Machine extends Model
{
    public $incrementing = false;
    protected $primaryKey = "machine_no";

     public function prod_model(){
       return $this->hasMany('App\Prod_model','machine_id','machine_no');
       }

    public function marque(){
      return $this->belongsTo('App\Marque');
    }
}

Prod_model 型号:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Prod_model extends Model
{
  public $incrementing = false;
  protected $primaryKey ="model_no";

  public function m_group(){
  return $this->hasMany('App\Group');
  }

  public function machine(){
    return $this->belongsTo('App\Machine');
  }
}

所以当我尝试使用此代码通过我的 Prod_models 访问我的组时

@foreach($machines as $machine)
          <ul>
            <li>
              @if($machine->cat_lvl == 1 && $machine->lowest_yn == 'Y')
               <input type="checkbox" id="{{ $machine->machine_no }}" /><label for="{{ $machine->machine_no }}">{{ $machine->cat_name }}</label>
               <ul>
                          @foreach($machine->prod_model as $model)
                        <li>
                          <input type="checkbox" id="{{ $model->model_no }}" /><label for="{{ $model->model_no }}">{{ $model->model_name}}</label>
                          <ul>
                            @foreach($model->m_group as $group)

                              <li>
                                <input type="checkbox" id="{{ $group->id }}" /><label for="{{  $group->id }}">{{ $group->group_name}}</label>
                                <ul>
                                @foreach($group->figure as $figure)
                                  <li><a onclick="load_main_content('{{ $figure->id }}');">{{ $figure->figure_name}}</a></li>
                                @endforeach
                                </ul>
                              </li>
                            @endforeach
                          </ul>
                        </li>
                          @endforeach
                      </ul>
                @endif
            </li>
          </ul>
          @endforeach

我收到此错误 Relationship 方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象。即使当我通过机器模型访问我的 prod_models 时它工作正常,但是当我试图通过我的 prod_models 访问我的组时它不起作用。那你能帮帮我吗?

【问题讨论】:

  • 请将您的完整错误放在问题上并请在 CodeStyle 上使用 psr-2
  • 您是否尝试将函数用作属性,$machine-&gt;prod_model 不应该是 $machine-&gt;prod_model()?
  • 在 laravel 框架上你可以同时使用它们, $machine->prod_model 执行一个关系并返回结果但是 $machine->prod_model() 返回关系然后你可以使用 ->get( )

标签: php laravel eloquent


【解决方案1】:

Prod_model 型号:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Prod_model extends Model
{
  public $incrementing = false;
  protected $primaryKey ="model_no";

  public function m_group(){
  return $this->hasMany('App\Group');
  }

  public function machine(){
    return $this->belongsTo('App\Machine','machine_no','machine_id');
  }
}

【讨论】:

  • 也许添加你为什么添加machine_no, machine_id?
  • 你能给出你的数据库的方案吗
  • 我会发布我的迁移
猜你喜欢
  • 2014-07-09
  • 2014-06-26
  • 1970-01-01
  • 2017-12-21
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多