Filtering the data does not work after adding the relational table: Laravel 5.2

蓝咒 提交于 2019-12-25 01:49:21

问题


I have below query that works fine.

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

In this above query I applied filter and works fine . Issue comes when I add more relation table and filter don't work at all.

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

Please suggest why filter does not work after adding the relational table.

Below is the table schema

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');
});

Below are the Models

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:


Assuming a Model structure that looks like this:

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

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

Then you can perform this query:

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


来源:https://stackoverflow.com/questions/37330253/filtering-the-data-does-not-work-after-adding-the-relational-table-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!