1

I am working on a very simple Laravel application that basically presents a graph based on an id. I believe I have set up the correct route using:

app\Http\routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('getProcessorData', array('uses' => 'HonoursController@getProcessorData'));
#Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData');
Route::get('getBandwidthData', array('uses' => 'HonoursController@getBandwidthData'));
Route::get('getMemoryData', array('uses' => 'HonoursController@getMemoryData'));

Route::resource('honours','HonoursController');

From there I went on to set up the correct function in the controller:

app\Http\Controllers\HonoursController.php

...
    public function getProcessorData()
    {
      $id = Input::get('host_id');
      return Response::json(HostProcessor::get_processor_formatted($id));
    }
...

Since this called a function from the HostProcessor model, I created this function below:

app\HostProcessor.php

...
    public static function get_processor_formatted($id)
    {
        $processor = self::findorfail($id);

        $json = array();
        $json['cols'] = array(
          array('label' => 'Time', 'type' => 'string'),
          array('label' => 'Kernel Space Usage', 'type' => 'number'),
          array('label' => 'User Space Usage', 'type' => 'number'),
          array('label' => 'IO Space Usage', 'type' => 'number')
        );

        foreach($processor as $p){
          $json['rows'][] = array('c' => array(
            array('v' => date("M-j H:i",strtotime($p->system_time))),
            array('v' => $p->kernel_space_time),
            array('v' => $p->user_space_time),
            array('v' => $p->io_space_time)
          ));
        }

        return $json;
    }
...

Finally I set up my AJAX function such as below:

resources/views/honours/partials/master.blade.php

...
     <script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.charts.load('current', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {
           var processor_usage = $.ajax({
              url:'getProcessorData',
              dataType:'json',
              async: false
           }).responseText;

           var p_options = {
              title: 'Processor Usage',
              width: 800,
              height: 400,
              hAxis: {
                 title: 'Time',
                 gridlines: {
                    count: 5 
                 }
              } 
          };
...

Now the issue I am having here is when I try and pass a value to the HostProcessor function it fails. I have tried passing an id value using the data attribute of AJAX. By doing this I had to update my route to Route::get('getProcessorData?{id}', array('uses' => 'HonoursController@getProcessorData')); but this still failed with a 404 error. I also tried just getting the value of host_id value using $id = Input::get('host_id'); and passing those to the HostProcessor function but that still failed with a 404 error. Any ideas why?

1
  • @OmeCoatl #Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData'); is currently commeted out ahd has no effect on the code Commented Mar 31, 2016 at 2:29

1 Answer 1

1

Try this:

// app\Http\routes.php
Route::get('getProcessorData/{id}', 'HonoursController@getProcessorData');

// app\Http\Controllers\HonoursController.php
//...
public function getProcessorData($id)
    {

      try {

          $processor = HostProcessor::get_processor_formatted($id);

          return response()->json($processor, 200);

      } catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) {

          return response()->json(['error' => $e->getMessage()], 404);

      } catch(\Exception $e) {

          return response()->json(['error' => $e->getMessage()], 500);
      }

    }
//...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.