jeudi 19 mai 2016

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

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

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1WEtClY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire