本文介绍了添加关系表后,无法过滤数据: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();
在上面的查询中,我应用了filter并运行良好.当我添加更多的关系表并且过滤器根本不起作用时,问题就来了.
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.
下面是表架构
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';
}
推荐答案
假设模型结构如下:
//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();
这篇关于添加关系表后,无法过滤数据:Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!