2016-05-06 8 views
1

をtblagegroup以下問題:Laravel 5.2以下

CREATE TABLE `tblagegroup` (
    `AgeGroupID` tinyint(3) UNSIGNED NOT NULL, 
    `MinAge` int(11) NOT NULL, 
    `MaxAge` int(11) NOT NULL 
); 

のスキーマがAgeGroupIDは、両方のテーブルに関連付けられているtblcontentlibrary

CREATE TABLE `tblcontentlibrary` (
    `ContentLibraryID` int(11) NOT NULL, 
    `AgeGroupID` tinyint(3) UNSIGNED NOT NULL 
); 

のスキーマであります。

私はMINAGE> = 2とMaxAgeのは以下= 4 <

ですが、私が試したものですtblcontentlibraryからそれらのレコードを取得しようとしています。

\App\Models\ContentLibraryModel 
::with('AgeGroup') 
->where('MinAge', '>=', $years) 
->where('MaxAge', '<=', $years) 
->get(); 

以下のエラーが発生しています。見つからない

コラム:1054不明な列 'MINAGE' 'where句' で(SQL: は、select * tblcontentlibraryからMinAge> = 0と< = )

は私が足りません何か?

答えて

1

あなたの関係は、this should work正しい場合:

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

** explode()は、パラメータ2が文字列であると想定しています**このエラーが発生しています。 – Pankaj

+0

ほぼ固定されています。しかし、テーブルtblagegroupからデータを返すことはありません。私はonlytblcontentlibraryからデータを取得しています – Pankaj

+0

適切な外部キーとの適切な関係を設定する必要があります。https://laravel.com/docs/5.1/eloquent-relationships#one-to-many –

0

は関係に基づいて収益を調整するようにwhereHasを使用してください。

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

これはのみがAgeGroup基準(whereHas)という収まり、またAgeGroup関係(with)を返しますモデルを返します。

さらに詳しい情報:
https://laravel.com/docs/5.1/eloquent-relationships

0

私の作業クエリは以下の通りです。

\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();