1

i am actually using graphs in my project for which i am passing dynamic data from controller to blade and then in the js script. The js function in the blade accept the array of objects and i dont know how to assign the data from the controller to that js function.

$data['typesBasedProperties'] = DB::table('properties')
            ->join('property_types', 'properties.property_type', 'property_types.id')
            ->select('property_types.types as label', DB::Raw('COUNT(properties.id) as value'))
            ->whereIn('properties.id', $property_ids)
            ->groupBy('label')
            ->get()
            ->toJson();

and this is the js function

var donutChart = function(){
            Morris.Donut({
                element: 'morris_donught',
                data: [
                    {
                    label: " Download Sales ",
                    value: 12,
                    }, {
                    label: " In-Store Sales ",
                    value: 30
                    }, {
                    label: " Mail-Order Sales ",
                    value: 20
                    }
                ],
                resize: true,
                redraw: true,
                colors: ['#2b98d6', 'rgb(59, 76, 184)', '#37d159'],
                //responsive:true,
                
            });
        }
1
  • Can you share your controller code? And do you have api call from your js (frontend)? And can you dd your query result? Commented Mar 2, 2022 at 7:00

2 Answers 2

1

I think you need to do something like this:

<script>
     var typesBasedProperties = {!! $typesBasedProperties !!}
</script>

After this you have typesBasedProperties object available in all your js code.

Check out laravel documentation for more info.

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

Comments

0

You can use ajax request to fetch data from laravel controller to javascript or JQuery.

here's some example

controller

function bbq(Request $r){
    $data = DB::select('CALL bukubesar('.$r->no.')');
    $node = DB::select('CALL infonode('.$r->no.')');

    return array(
        'data' => $data,
        'node' => $node
    );
}

route (I'm using Group)

Route::controller(CPembukuan::class)->group(function () {
    Route::get ('/pembukuan-besar',          'bukubesar');
    Route::get ('/pembukuan-besar/query',    'bbq'      );
    Route::get ('/pembukuan-labarugi',       'labarugi' );
    Route::get ('/pembukuan-labarugi/query', 'lrq'      );
});

ajax (JQuery) on html or blade file

//a variable to pass to controller (optional)
val = $('#kode').val();

$.ajax({
    type: "get",
    url: "/pembukuan-besar/query",
    data: {
        'no': val // the data represented as 'no'
    },
    success: function(data) {
       console.log(data); // to view the data in console
       //do anything you want here
    }
});

Comments

Your Answer

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