CREATE TABLE `tblprojecttype` (
`ProjectTypeID` int(11) NOT NULL,
`ProjectType` varchar(30) NOT NULL,
`Description` varchar(500) NOT NULL,
`IsActive` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `tblprojecttypecurrencyprice` (
`ProjectTypeCurrencyID` int(11) NOT NULL,
`CurrencyID` int(11) NOT NULL,
`ProjectTypeID` int(11) NOT NULL,
`Price` decimal(10,0) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `tblcurrency` (
`CurrencyID` int(11) NOT NULL,
`Currency` varchar(100) NOT NULL,
`IsActive` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
class Currency_Model extends Model
{
protected $table = "tblcurrency";
protected $primaryKey = "CurrencyID";
public $timestamps = false;
}
class ProjectType_Model extends Model
{
protected $table = "tblprojecttype";
protected $primaryKey = "ProjectTypeID";
public $timestamps = false;
public function projecttypecurrencyprice()
{
return $this->hasOne('\App\Models\ProjectTypeCurrencyPrice_Model',
"ProjectTypeID");
}
}
class ProjectTypeCurrencyPrice_Model extends Model
{
protected $table = "tblprojecttypecurrencyprice";
protected $primaryKey = "ProjectTypeCurrencyID";
public $timestamps = false;
public function Currency()
{
return $this->belongsTo('\App\Models\Currency_Model', "CurrencyID");
}
}
CurrencyID
关系和ProjectTypeID
关系
在我的Laravel 5.1代码中,我试图在sql语句下面执行,以便我可以为每个projecttypecurrencyprice records
获取ProjectType record
。最后,它还应显示每个currency Table
记录的projecttypecurrencyprice
记录
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with("projecttypecurrencyprice.Currency")
->get();
我正在获取每个projecttypecurrencyprice
记录的所有货币记录。我只想要那些货币ID = 1
答案 0 :(得分:0)
在获取相关的** projecttypecurrencyprice **对象时,您需要应用其他条件。您可以使用 whereHas()方法执行此操作。以下代码应该可以解决问题:
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with(["projecttypecurrencyprice.Currency" => function($query) {
$query->where('CurrencyID', 1);
}])
->get();
答案 1 :(得分:0)
如果我没有遗漏某些东西,不应该做一个简单的where() clause in your eloquent query builder伎俩吗?
像这样:
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with("projecttypecurrencyprice.Currency")
->where('id', 1)
->get();
(当然没有经过测试,请不要打我)