Skip to content
Advertisement

Laravel 5.4 hasManyTrough ‘Call to undefined method IlluminateDatabaseQueryBuilder::hasManyTrough()’

I am struggling to see, where I went wrong. It seams easy, I followed Laravel instructions https://laravel.com/docs/5.4/eloquent-relationships#has-many-through , but clearly I need someone more familar with this sort of code as whenever I try to fetch $stagingsystem-stagesubtype I get error

BadMethodCallException with message ‘Call to undefined method >IlluminateDatabaseQueryBuilder::hasManyTrough()’

Can someone help?

StagingSystems

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateStagingSystemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('staging_systems', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('staging_systems');
    }
}

StageName

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateStageNamesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stage_names', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('staging_system_id')->unsigned();
            $table->foreign('staging_system_id')->references('id')->on('staging_systems');

            $table->string('name')->unique;
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('stage_names');
    }
}

StageSubType

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateStageSubTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stage_sub_types', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('stage_name_id')->unsigned();
            $table->foreign('stage_name_id')->references('id')->on('stage_names');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('stage_sub_types');
    }
}

MODELS

StagingSystem

<?php

namespace App;

use AppStagingSystem;
use IlluminateDatabaseEloquentModel;

class StagingSystem extends Model
{
    protected $guarded = ['id'];

    public function stagename()
    {
        return $this->hasMany('AppStageName');
    }

    public function stagesubtype()
    {
        return $this->hasManyTrough('AppStageSubType','AppStageName');
    }

}

StageName

<?php

namespace App;

use AppStageName;
use IlluminateDatabaseEloquentModel;

class StageName extends Model
{
  protected $guarded = ['id'];

    public function stagingsystem()
    {
        return $this->belongsTo('AppStagingSystem','id');
    }

    public function stagesubtype()
    {
        return $this->hasMany('AppStageSubType');
    }

}

StageSubType

<?php

namespace App;

use AppStageSubType;
use IlluminateDatabaseEloquentModel;

class StageSubType extends Model
{
      protected $guarded = [
        'id'
    ];

    public function stagename()
    {
        return $this->belongsTo('AppStageName');
    }

     public function stagingsystem()
    {
        return $this->belongsTo('AppStagingSystem');
    }


}

Advertisement

Answer

You have mistyped HasManyThrough as hasManyTrough.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement