代码之家  ›  专栏  ›  技术社区  ›  Michael Emerson

即使传递ID,Laravel雄辩查询也始终返回所有列

  •  0
  • Michael Emerson  · 技术社区  · 6 年前

    我绞尽脑汁已经有一段时间了,我想不出为什么会这样。

    基本上,我有一个满是订户列表名的表,我需要为每个订户的详细信息页检索一个订户列表名。但是,无论我如何尝试检索数据,它总是返回表中的每一行。我在这个模型上有两个关系,因为订户列表中有许多用户,可能还有许多附加的组织。

    以下是我当前尝试检索单个记录时使用的查询:

    $query = SubscriberList::with($loadRelations)->where('id', $subscriberListId)->firstOrFail();
    

    这将返回所有行。

    我也尝试过:

    $query = SubscriberList::with($loadRelations)->findOrFail($subscriberListId);
    

    结果相同。

    我的关系是 users organisations 在下标模型中分别设置和,如下所示:

    public function users()
    {
        return $this->hasManyThrough(User::class,SubscriberListsUsers::class,'user_id','id');
    }
    
    public function organisations()
    {
        return $this->hasManyThrough(Organisation::class, SubscriberListsOrganisations::class,'organisation_id','id');
    }
    

    每个都使用不同的数据透视表来存储关系。

    我不确定为什么我得到了我得到的结果——有人能解释一下吗?我已经很久没有用雄辩的语言了,所以我可能只是错过了一些显而易见的东西。

    编辑:

    结果 dd($query) :

    SubscriberList {#451
      #fillable: array:1 [
        0 => "name"
      ]
      #connection: "pgsql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [
        "id" => 10
        "name" => "Michael"
        "created_at" => "2018-08-07 13:59:24"
        "updated_at" => "2018-08-07 13:59:24"
      ]
      #original: array:4 [
        "id" => 10
        "name" => "Michael"
        "created_at" => "2018-08-07 13:59:24"
        "updated_at" => "2018-08-07 13:59:24"
      ]
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:2 [
        "users" => Collection {#440
          #items: []
        }
        "organisations" => Collection {#454
          #items: []
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
    }
    

    编辑:架构 用户 和数据透视表:

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('colour');
        $table->integer('organisation_id')->unsigned()->nullable();
        $table->string('requested_organisation')->nullable();
        $table->integer('sso_id')->nullable();
        $table->string('email')->unique();
        $table->string('job_title')->nullable();
        $table->string('telephone')->nullable();
        $table->boolean('confirmed')->default(false);
        $table->rememberToken();
        $table->timestamps();
    
        $table->foreign('organisation_id')
            ->references('id')
            ->on('organisations')
            ->onUpdate('cascade')
            ->onDelete('set null');
    });
    
    Schema::create('subscriber_lists_users', function(Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->integer('subscriber_list_id');
        $table->integer('user_id');
    
        $table->foreign('subscriber_list_id')
            ->references('id')
            ->on('subscriber_lists')
            ->onUpdate('cascade')
            ->onDelete('cascade');
    
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');
    });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Jonas Staudenmeir    6 年前

    $query->get() 执行新查询,只返回 $query .

    users 使用错误的关系类型:

    public function users()
    {
        return $this->belongsToMany(User::class, 'subscriber_lists_users');
    }