This is my Messages Model , this table has two Foreign Keys id_from and id_to , both from the User table. How can I establish this by using eloquent in 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.