简体   繁体   English

使用口才的ORM

[英]Using Eloquent ORM

This is my Messages Model , this table has two Foreign Keys id_from and id_to , both from the User table. 这是我的消息模型 ,此表有两个外键id_fromid_to ,都来自User表。 How can I establish this by using eloquent in Laravel? 我该如何在Laravel中使用雄辩的方法来确定这一点?

namespace App;

use Illuminate\Database\Eloquent\Model;

class Messages extends Model
{


    public function user(){
        return $this->hasMany('App\User','id','id_from');
    }

    public function user(){
        return $this->hasMany('App\User','id','id_to');
    }

    protected $primaryKey = 'id';
    protected $table = 'messages';
    protected $fillable = ['id_to','id_from','message'];
    public $timestamps = false;

}

You should use tow diferent functions for that : 您应该为此使用两个不同的函数:

public function userFrom(){
    return $this->hasMany('App\User','id','id_from');
}

public function userTo(){
    return $this->hasMany('App\User','id','id_to');
}

And one more thing is that the name of the function must be in camelCase 还有一件事是该函数的名称必须camelCase

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

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