Php 为父表应用where子句时出现问题:Laravel 5.2

Php 为父表应用where子句时出现问题:Laravel 5.2,php,laravel,laravel-5.1,laravel-5.2,Php,Laravel,Laravel 5.1,Laravel 5.2,以下是tblagegroup的架构 CREATE TABLE `tblagegroup` ( `AgeGroupID` tinyint(3) UNSIGNED NOT NULL, `MinAge` int(11) NOT NULL, `MaxAge` int(11) NOT NULL ); 以下是tblcontentlibrary的架构 CREATE TABLE `tblcontentlibrary` ( `ContentLibraryID` int(11) NOT NULL,

以下是tblagegroup的架构

CREATE TABLE `tblagegroup` (
  `AgeGroupID` tinyint(3) UNSIGNED NOT NULL,
  `MinAge` int(11) NOT NULL,
  `MaxAge` int(11) NOT NULL
);
以下是tblcontentlibrary的架构

CREATE TABLE `tblcontentlibrary` (
  `ContentLibraryID` int(11) NOT NULL,
  `AgeGroupID` tinyint(3) UNSIGNED NOT NULL
);
AgeGroupID在两个表中都关联

我试图从tblcontentlibrary中获取这些记录,其中MinAge>=2,MaxAge为('MinAge','>=',$years)
->其中('MaxAge','如果您的关系正确,:

\App\Models\ContentLibraryModel::with(['AgeGroup'=>函数($query)use($years){

$query->where('MinAge','>=',$years)->where('MaxAge','使用
whereHas
根据关系来调整返回条件

\App\Models\ContentLibraryModel::with('AgeGroup')
    ->whereHas('AgeGroup', function ($query) use ($years) {
        $query->where('MinAge', '>=', $years);
        $query->where('MaxAge', '<=', $years);
    })
    ->get();
\App\Models\ContentLibraryModel::with('AgeGroup')
->whereHas('AgeGroup',function($query)use($years){
$query->where('MinAge','>=',$years);

$query->where('MaxAge','我的工作查询在下面

\App\Models\ContentLibraryModel
    ::selectRaw('CL.*, AG.MinAge, AG.MaxAge')
    ->from('tblcontentlibrary as CL')
    ->join('tblagegroup as AG', 'AG.AgeGroupID', '=', 'CL.AgeGroupID')
    ->where('AG.MinAge', '<=', $years)
    ->where('AG.MaxAge', '>=', $years)
    ->get();
\App\Models\ContentLibraryModel
::selectRaw('CL.*,AG.MinAge,AG.MaxAge')
->from('tblcontentlibrary as CL')
->join('tblagegroup as AG','AG.AgeGroupID','=','CL.AgeGroupID')
->其中('AG.MinAge','=',$years)
->get();

explode()预期参数2是字符串,对象,因为我现在得到了这个错误。几乎修复了。但它现在不从表tblagegroup返回数据。我从OnlyBlContentLibrary获得数据要使其工作,您应该使用正确的外键设置正确的关系,如下所示:当我这样编写数据时,它显示正确的数据
\App\Models\ContentLibraryModel::with(['AgeGroup'=>函数($query)use($years){$query->where('MinAge','>=',$years)->where('MaxAge',我看不到实际的表数据,但问题可能出在
=
和'
\App\Models\ContentLibraryModel::with('AgeGroup')
    ->whereHas('AgeGroup', function ($query) use ($years) {
        $query->where('MinAge', '>=', $years);
        $query->where('MaxAge', '<=', $years);
    })
    ->get();
\App\Models\ContentLibraryModel
    ::selectRaw('CL.*, AG.MinAge, AG.MaxAge')
    ->from('tblcontentlibrary as CL')
    ->join('tblagegroup as AG', 'AG.AgeGroupID', '=', 'CL.AgeGroupID')
    ->where('AG.MinAge', '<=', $years)
    ->where('AG.MaxAge', '>=', $years)
    ->get();