/**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show(Variable $variable, Request $request)
 {
     if ($request->ajax()) {
         //use query builder instead of eloquent
         $rawData = DB::table('data_values')->join('entities', 'data_values.fk_ent_id', '=', 'entities.id')->join('times', 'data_values.fk_time_id', '=', 'times.id')->where('data_values.fk_var_id', $variable->id)->get();
         $data = [];
         $index = 1;
         foreach ($rawData as $d) {
             if (!array_key_exists($d->name, $data)) {
                 $obj = new \StdClass();
                 $obj->id = $index;
                 $obj->key = $d->name;
                 $obj->values = [];
                 $data[$d->name] = $obj;
                 $index++;
             }
             $data[$d->name]->values[] = ["x" => intval($d->label), "y" => intval($d->value)];
         }
         return ['success' => true, 'data' => ['variable' => $variable, 'data' => $data]];
     } else {
         //data
         /*$source = DB::table( 'data_values' )
         		->leftJoin( 'entities', 'data_values.fk_ent_id', '=', 'entities.id' )
         		->leftJoin( 'times', 'data_values.fk_time_id', '=', 'times.id' )
         		->select( DB::raw( 'data_values.*, times.label, entities.name' ) );*/
         $source = DataValue::grid()->where('fk_var_id', '=', $variable->id);
         //$source = DataValue::with( array('Entity','Time') )->where( 'fk_var_id', '=', $variable->id );
         $entityList = DataValue::where('fk_var_id', '=', $variable->id)->lists('fk_var_id');
         $entities = Entity::whereIn('id', $entityList);
         //datagrid & filter
         $filter = \DataFilter::source($source);
         $filter->attributes(array('class' => 'form-inline'));
         $filter->add('value', 'Value', 'text');
         $entitiesList = Entity::lists('name', 'name');
         $entitiesList[''] = 'All';
         $filter->add('Entities.name', 'Entity', 'select')->options($entitiesList);
         $filter->add('Times.label', 'Time', 'text');
         $filter->submit('search');
         $filter->build();
         $grid = \App\Components\BatchDataGrid::source($filter);
         $grid->add('id', 'ID', true)->style('width:100px');
         $grid->add('value', 'Value', true);
         $grid->add('name', 'Entity', true);
         $grid->add('label', 'Time', true);
         $grid->add('description', 'Description');
         $grid->add('<a href="' . route('values.index') . '/{{$id}}/edit">Edit</a>', 'Edit');
         $grid->paginate(50);
         //is csv export?
         if (Input::has('export') && Input::get('export') == 'csv') {
             return $grid->buildCSV('export_variable', 'Y-m-d.His');
         }
         //construct csv export url
         $exportUrl = $request->fullUrl() . '&export=csv';
         return view('variables.show', compact('variable', 'values', 'grid', 'filter', 'exportUrl'));
     }
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit(DataValue $dataValue)
 {
     $entities = Entity::lists('name', 'id');
     $datasources = Datasource::lists('name', 'id');
     return view('values.edit', compact('dataValue', 'entities', 'datasources'));
 }