Laravel有很多自我的标准吗?

How can I create a hasMany self relationship with where criteria?

e.g., this is what I want to do:

class Payment extends \Illuminate\Database\Eloquent\Model {
    public function refunds() {
        return $this->hasMany(self::class, 'transaction_id', 'transaction_id')
             ->where('this.method','=','that.method')
             ->where('that.amount','<',0);
    }
}

But I don't know how to give the table two different aliases so that I can set the WHERE criteria.

N.B. in my example, a "refund" is just a negative payment. They will both have the same transaction ID and method.

How about construct the HasMany class your self?

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    public function refunds() {
        $fKey = 'transaction_id';
        $instance = new static;
        $instance = $instance->newQuery()
                      ->where('this.method','=','that.method')
                      ->where('that.amount','<',0);      

        return new HasMany(
            $instance, $this, $instance->getTable().'.'.$fKey, $fKey
        );
    }
}

You can set your relation normal and put the condition at controller

$user = User::with('Profile')->where('status', 1)->whereHas('Profile',    function($q){
            $q->where('gender', 'Male');
        })->get();