大家好我需要帮助!!! 所以我有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
我收到此错误R 关系方法必须返回类型为Illuminate \ Database \ Eloquent \ Relations \ Relation 的对象。即使它通过机器模型访问我的prod_models时也能正常工作,但是当我试图通过我的prod_models访问我的组时它无法正常工作。那你可以帮帮我吗?
答案 0 :(得分:0)
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');
}
}