0
votes

Hello guys i need help !!! So i have 3 models : Machine , Prod_model and Group Machine Model :

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

So when i'm trying to access my groups via my Prod_models with this code

@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

I'm getting this error Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation . even that it works normaly when im accessing my prod_models via the machine model , but its not working when i'm trying to accessing my groups via my prod_models . so can you help me please ?

1
please put your complete error on question & please use psr-2 on CodeStyleHonarkhah
Arent you trying to use a function as an attribute shouldnt $machine->prod_model be $machine->prod_model()?Jesse de gans
on laravel framework you can use both of them , $machine->prod_model execute a relation and return result of that but $machine->prod_model() return relation and then you can use ->get()Honarkhah

1 Answers

0
votes

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