2016-12-22 7 views
1

は、だから私は4つのテーブルを持っている複数の列と複数の行をマージ:Laravelは

  • 実験(テーブル) - ID
  • feature_classes(列) - ID、EXPERIMENT_ID、タイトル
  • エンティティ(行) - ID ID、ENTITY_ID、feature_class_id、値

Iは、これらの4つのテーブルからテーブルを構築する必要がある - 、

  • 機能(細胞)EXPERIMENT_ID。

    私はこれを試してみました:entitiesを選択:

    $experiment_id = $request->input('experiment_id'); 
        $feature_classes = FeatureClass::where('experiment_id', $experiment_id)->select('title', 'id')->get(); 
    
        $select = [ 
         'entities.id', 
         'entities.prediction', 
         'entities.result' 
        ]; 
    
        foreach ($feature_classes as $f) { 
         $select[] = $f->id . ".value AS " .$f->id; 
        } 
    
        $entities = DB::table('entities') 
         ->where('experiment_id', $experiment_id); 
    
        foreach ($feature_classes as $f) { 
         $entities = $entities->leftJoin('features AS ' . $f->id, function ($join) use ($f){ 
          $join->on($f->id . '.entity_id', '=', 'entities.id') 
           ->where($f->id . '.feature_class_id', $f->id); 
         }); 
        } 
    
        return $entities 
         ->select($select) 
         ->get(); 
    

    しかし、私の努力は、このエラーメッセージSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1 ? left join機能で報われはライン1(SQLで=エンティティ. ID FE」をENTITY_ID。 id,,entities,result,1,value,1,2value2からentitiesに残してください。features11として残してください。 entity_id = entitiesidおよび1feature_class_id 1が残ってfeaturesとして22になります。 entity_id = entitiesidおよび2。 = 1) `

  • 答えて

    0

    私はあなたがするべきではないと主張したいと思います。あなたはモデル内の多対多リレーションシップを追加し、残りのすべてを実行するために雄弁を使用してこれにアプローチする必要があります。たとえば、フィーチャーモデルの場合:

    public function Classes(){ 
        return $this->belongsToMany('App\Classes', 'feature_classes', 'feature_class_id', 'id'); 
    } 
    

    次に、experiment_idでリンクしているクラスとエンティティの関係を定義する類似のものです。そして、あなたはここに

    $entities = Features::where('experiment_id', $experiment_id)->Classes->Entities; 
    
    return $entities 
    

    詳しい情報と同様にネイティブ雄弁な機能を使用して、必要なデータにアクセスできるようにする必要があります

    https://laravel.com/docs/5.3/eloquent-relationships#many-to-many

    +0

    あなたは「アプリケーション\クラス」とはどういう意味ですか? –

    +0

    関連するモデル –

    +0

    1. 'Feature'は' FeatureClass'と多少の関係がありません。 2. 'Feature'に' experiment_id'カラムがありません。 3. 'FeatureClass'は' Entity'に直接関係しません。 4.あなたが関係を正しく取得したとしても、あなたの答えが 'フィーチャー'テーブルのデータとフィーチャーの説明がなく、 'entity'テーブルのデータだけを返してくれると確信しています。 –