简体   繁体   English

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

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

I have 2 tables linked by belongsToMany relationship and a pivot table and it's working fine. 我有2个表,分别由belongsToMany关系和数据透视表链接,并且工作正常。 Then I tried referencing a non-primary key instead of a primary key in the pivot table. 然后,我尝试在数据透视表中引用非主键而不是主键。 However when I run my code in my controller, laravel does not return any result. 但是,当我在控制器中运行代码时,laravel不会返回任何结果。

I guess it might be because in a belongsToMany relationship, laravel automatically thinks the foreign keys in pivot tables are referencing to the primary keys and that's why my code doesn't work. 我猜这可能是因为在一个AboutToMany关系中,laravel自动认为数据透视表中的外键引用了主键,这就是为什么我的代码不起作用的原因。

How do I change laravel's logic? 如何更改laravel的逻辑?

ps I'm able to get those records by using ConversationsUser model but it accesses the database twice in order to get the results (it checks the conversation table first then checks the pivot table). ps我能够使用ConversationsUser模型来获取那些记录,但是它两次访问数据库以获取结果(它先检查对话表,然后再检查数据透视表)。 That would be my last resort if I can't find other solutions. 如果找不到其他解决方案,那将是我的不得已的选择。


CONTROLLER CONTROLLER

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

It returns an empty array when there are actually valid records in the pivot table. 当数据透视表中实际有有效记录时,它将返回一个空数组。 I need it to return those records. 我需要它来返回这些记录。

MODELS 楷模

User 用户

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

Conversations note:as you can see, I have tried adding 2 more parameters as suggested in this answer but still doesn't work. 会话注释:如您所见,我已尝试按照此答案中的建议再添加2个参数,但仍然无法正常工作。

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

MIGRATIONS MIGRATIONS

User 用户

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

Conversations 对话

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

Try change from this 尝试从这里改变

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

to this 对此

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

The second argument of belongsToMany is table name of your junction table according to [0] EmiratesToMany的第二个参数是根据[0]的联结表的表名

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

Also because you use different key for Conversations table, try to change it in your Conversation model by adding this two variable inside your model class : 另外,因为您为“对话”表使用了不同的键,所以请尝试在“对话”模型中通过在模型类中添加以下两个变量来对其进行更改:

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