添加关系表后,过滤数据不起作用:Laravel 5.2

时间:2016-05-19 17:30:30

标签: php laravel laravel-5 laravel-5.1 laravel-5.2

我在下面的查询工作正常。

\App\Models\Membership\MembershipModel
    ::with(['Packages' => function($query){
        $query->where('ExperienceID', \Auth::user()->ExperienceID)
                ;
    }])
    ->get();

在上面的查询中,我应用了过滤器并且工作正常。当我添加更多关系表和过滤器时,问题就出现了。

\App\Models\Membership\MembershipModel
    ::with(['Packages' => function($query){
        $query->where('ExperienceID', \Auth::user()->ExperienceID)
                ;
    }])
    ->with('Packages.AssetTypes')
    ->get();

请在添加关系表后建议为什么过滤器不起作用。

下面是表格架构

Schema::create('tblmembership', function (Blueprint $table) {
    $table->integer('MembershipID')->unsigned()->autoIncrement();
    $table->string('MembershipName', 20);
});

Schema::create('tblyearsofexperience', function (Blueprint $table) {
    $table->tinyInteger('ExperienceID')->unsigned()->autoIncrement();
    $table->integer('MinAge');
    $table->integer('MaxAge');
});

Schema::create('tblmembershippackage', function (Blueprint $table) {
    $table->integer('PackageID')->unsigned()->autoIncrement();
    $table->string('PackageName', 50);
    $table->tinyInteger('ExperienceID')->unsigned();
    $table->integer('MembershipID')->unsigned();

    $table->foreign('ExperienceID')->references('ExperienceID')->on('tblexperience');
    $table->foreign('MembershipID')->references('MembershipID')->on('tblmembership');
});
Schema::create('tblassettypes', function (Blueprint $table) {
    $table->tinyInteger('AssetTypeID')->unsigned()->autoIncrement();
    $table->string('AssetType', 20);
});

Schema::create('tblmembershippackageassettypes', function (Blueprint $table) {
    $table->integer('MembershipPackageAssetTypeID')->unsigned()->autoIncrement();
    $table->integer('PackageID')->unsigned();
    $table->tinyInteger('AssetTypeID')->unsigned();

    $table->foreign('PackageID')->references('PackageID')->on('tblmembershippackage');
    $table->foreign('AssetTypeID')->references('AssetTypeID')->on('tblassettypes');
});

以下是模型

class ExperienceModel extends Model
{
    public $table = 'tblexperience';
    public $primaryKey = 'ExperienceID';
    public $timestamps = true;
}

class MembershipModel extends Model
{
    public $table = 'tblmembership';
    public $primaryKey = 'MembershipID';
    public $timestamps = true;

    public function Packages()
    {
        return $this->hasMany('\App\Models\MembershipPackageModel', "MembershipID", "MembershipID");
    }
}


class MembershipPackageModel extends Model
{
    public $table = 'tblmembershippackage';
    public $primaryKey = 'PackageID';

    public function Experience() {
        return $this->hasOne('\App\Models\ExperienceModel', 'ExperienceID', 'ExperienceID');
    }

    public function Membership() {
        return $this->belongsTo('\App\Models\MembershipModel', 'MembershipID', 'MembershipID');
    }

    public function AssetTypes()
    {
        return $this->hasMany('\App\Models\MembershipPackageAssetTypeModel', 
                "PackageID", "PackageID");
    }
}



class MembershipPackageAssetTypeModel extends Model
{
    public $table = 'tblmembershippackageassettypes';
    public $primaryKey = 'MembershipPackageAssetTypeID';

}

1 个答案:

答案 0 :(得分:1)

假设模型结构如下所示:

//MembershipModel
public function Packages(){
    return $this->hasMoney('App\Models\Packages');
}

//PackagesModel
public function AssetTypes(){
    $this->hasMany('App\Models\AssetTypes);
}

然后您可以执行此查询:

\App\Models\Membership\MembershipModel
::with([
    'Packages' => function($query){
        $query->where('ExperienceID', \Auth::user()->ExperienceID)
            ;
    }, 
    'Packages.AssetTypes'
])
->get();