首页 > 解决方案 > Laravel eloquent query always returns all columns even when passing id

问题描述

I have been racking my brain for a while now and I cannot come up with a reason why this is happening.

Basically, I have a table full of subscriber list names and I need to retrieve a single one for the details page of each. However, no matter how I try to retrieve the data it always returns every single row in the table. I have two relations on this model, as the subscriber lists have many users and, possibly, many organisations attached.

Here is the query I am currently using to attempt to retrieve the single record:

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

This returns all rows.

I've also tried:

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

With the same results.

My relations are users and organisations respectively and are set up as follows in the SubscriberList model:

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

Each one using a different pivot table to house the relations.

I am unsure why I'm getting the result I'm getting - can anyone shed any light on this? I haven't been using Eloquent for long so I may just have missed something obvious here.

EDIT:

The result of 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 => "*"
  ]
}

EDIT: Schemas for users and pivot table:

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

标签: laraveleloquent

解决方案


$query->get() executes a new query, just return $query.

users uses the wrong relationship type:

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

推荐阅读