繁体   English   中英

Laravel:如何从数据透视表中获取其外键未引用主键的记录?

[英]Laravel : How do I get records from pivot table whereby its foreign key does not reference a primary key?

我有2个表,分别由belongsToMany关系和数据透视表链接,并且工作正常。 然后,我尝试在数据透视表中引用非主键而不是主键。 但是,当我在控制器中运行代码时,laravel不会返回任何结果。

我猜这可能是因为在一个AboutToMany关系中,laravel自动认为数据透视表中的外键引用了主键,这就是为什么我的代码不起作用的原因。

如何更改laravel的逻辑?

ps我能够使用ConversationsUser模型来获取那些记录,但是它两次访问数据库以获取结果(它先检查对话表,然后再检查数据透视表)。 如果找不到其他解决方案,那将是我的不得已的选择。


CONTROLLER

$loginuser = User::find(Auth::user()->id);
$conversations = $loginuser->conversations;
dd($conversations->toArray());

当数据透视表中实际有有效记录时,它将返回一个空数组。 我需要它来返回这些记录。

楷模

用户

public function conversations(){
   return $this->belongsToMany('Conversations')->withPivot('opened','last_seen');
}

会话注释:如您所见,我已尝试按照此答案中的建议再添加2个参数,但仍然无法正常工作。

public function users(){
    return $this->belongsToMany('User','conversations','conversations_id');
}

MIGRATIONS

用户

Schema::create('users',function($table)
        {
            $table->increments('id');
            $table->string('email')->unique();
            $table->string('password',100);
            $table->string('name',150);
            $table->string('usertype',50);
            $table->boolean('block');
            $table->string('remember_token',100);
            $table->timestamp('lastlogin_at');
            $table->timestamps();
            $table->softDeletes();
        });

对话

        Schema::create('Conversations', function(Blueprint $table)
        {
            $table->increments('id');
            $table->bigInteger('conversations_id')->unsigned()->index();
            $table->string('name',100);
            $table->timestamps();
            $table->bigInteger('microseconds');
        });

conversations_user

        Schema::create('conversations_user', function(Blueprint $table)
        {
            $table->increments('id')->unsigned();
            $table->bigInteger('conversations_id')->unsigned()->index();
            $table->foreign('conversations_id')->references('conversations_id')->on('conversations')->onDelete('cascade');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
            $table->bigInteger('microseconds');
            $table->boolean('opened');
            $table->dateTime('last_seen');
        });

尝试从这里改变

public function users(){
    return $this->belongsToMany('User','conversations','conversations_id');
}

对此

public function users(){
    return $this->belongsToMany('User','conversations_user','user_id','conversations_id');
}

EmiratesToMany的第二个参数是根据[0]的联结表的表名

[0] http://laravel.com/api/class-Illuminate.Database.Eloquent.Relations.BelongsToMany.html

另外,因为您为“对话”表使用了不同的键,所以请尝试在“对话”模型中通过在模型类中添加以下两个变量来对其进行更改:

class Conversation extends Eloquent {
    protected $primaryKey = "conversation_id";
    public $incrementing = false; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM