doubingqi5829
doubingqi5829
2016-07-18 18:50
浏览 38
已采纳

Laravel Relation belongsTo id无法识别

I have those schemas:

Schema::create('tickets', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('ticket_reason_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->timestamps();
    $table->foreign('ticket_reason_id')->references('id')->on('ticket_reasons')->onDelete('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

Schema::create('ticket_reasons', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

And those defined relationships

class Ticket extends Model
{

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function reason()
    {
        return $this->belongsTo(TicketReason::class, 'ticket_reason_id');
    }

}

The "reason" method works but I had to manually add the ID. Why do I have to add the id if I'm following the convention? (or at least I think so). It follows the same convention than the user_id column and it doesnt need the id setter.

图片转代码服务由CSDN问答提供 功能建议

我有这些模式:

  Schema :: create(  'ticket',function(Blueprint $ table){
 $ table-> incrementments('id'); 
 $ table-> integer('ticket_reason_id') - > unsigned(); 
 $ table-  >整数('user_id') - > unsigned(); 
 $ table-> timestamps(); 
 $ table-> foreign('ticket_reason_id') - > references('id') - &gt  ; on('ticket_reasons') - > onDelete('cascade'); 
 $ table-> foreign('user_id') - > references('id') - > on('users') - &gt  ; onDelete('cascade'); 
}); 
 
Schema :: create('ticket_reasons',function(Blueprint $ table){
 $ table-> increments('id'); 
 $ table  - > string('name'); 
 $ table-> timestamps(); 
}); 
   
 
 

以及那些已定义的关系

  class Ticket扩展Model 
 {
 
公共函数user()
 {
返回$ this-> belongsTo(User :: class); 
} \  n 
公共函数原因()
 {
返回$ this-> belongsTo(TicketReason :: class,'tic  ket_reason_id'); 
} 
 
} 
   
 
 

“reason”方法有效,但我必须手动添加ID。 如果我遵循约定,为什么我必须添加id? (或者至少我是这么认为的)。 它遵循与user_id列相同的约定,并且它不需要id setter。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglie9067
    donglie9067 2016-07-19 18:17
    已采纳

    So, I'm going to fault Laravel's Eloquent documentation on this one...since, it's really not too clear on how the foreign key is auto-generated. I did find this, from the Eloquent Relationships page, though (emphasis mine):

    Eloquent will try to match the user_id from the Phone model to an id on the User model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id.

    Contrary to what one (you, myself, etc.) may assume, the foreign key is not generated by the foreign class's name. Instead, it uses the name of the method that your ::belongsTo() (or hasOne(), etc.) call is in.


    This means that public function reason() {} needs to be changed to public function ticket_reason() {}. Or, of course, you could change your column name to reason_id.

    点赞 评论

相关推荐