【问题标题】:Laravel 5.4 hasManyTrough 'Call to undefined method Illuminate\Database\Query\Builder::hasManyTrough()'Laravel 5.4 hasManyTrough '调用未定义的方法 Illuminate\Database\Query\Builder::hasManyTrough()'
【发布时间】:2017-04-02 17:11:16
【问题描述】:

我正在努力寻找,我哪里出错了。接缝很简单,我按照 Laravel 指令 https://laravel.com/docs/5.4/eloquent-relationships#has-many-through 进行操作,但显然我需要更熟悉此类代码的人,因为每当我尝试获取 $stagingsystem-stagesubtype 时都会出错

带有消息“调用未定义方法>Illuminate\Database\Query\Builder::hasManyTrough()”的BadMethodCallException

有人可以帮忙吗?

分期系统

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

舞台名称

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

阶段子类型

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

模型

分期系统

<?php

namespace App;

use App\StagingSystem;
use Illuminate\Database\Eloquent\Model;

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

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

    public function stagesubtype()
    {
        return $this->hasManyTrough('App\StageSubType','App\StageName');
    }

}

舞台名称

<?php

namespace App;

use App\StageName;
use Illuminate\Database\Eloquent\Model;

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

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

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

}

阶段子类型

<?php

namespace App;

use App\StageSubType;
use Illuminate\Database\Eloquent\Model;

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

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

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


}

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    对于遇到类似问题的任何人,如果您尝试使用 withPivot() 访问模型关系,则可能会出现此错误消息,但中间关系扩展为 Model 而不是 Pivot

    【讨论】:

      【解决方案2】:

      您将HasManyThrough 输入错误为hasManyTrough

      【讨论】:

      • 我不太明白你的意思。作为链接的页面显示名称和用法是 hasManyThrough。想多解释一下吗?
      • @rpz 仔细看,你拼错了。
      • 总是那些昏迷和拼写错误让我发疯。谢谢@Mihai 给我一双新鲜的眼睛!
      猜你喜欢
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2014-04-25
      相关资源
      最近更新 更多