Laravel5.0:将值从控制器方法传递到公共文件夹中的 .php 文件

问题描述 投票:0回答:3

我想将一个值从我的控制器传递到 public 文件夹内的 .php 文件。 这是我的控制器部分

<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller {

public function barChart(){

    $barChart= DB::table('clients')
        ->select('clients.ClientName','clients.Price')
        ->get();
    //want to pass this $barChart variable
}

.php 文件的位置是

/dist/chart/clients.php

我想在clients.php文件中包含这个$barChart变量并执行进一步的操作。

更新1: 这是视图部分

    <div class="panel-body" id="barChart">

    </div>



    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
        $.ajax({
        //url: "{{URL::asset('/dist/chart/clients.txt')}}", //this works, I saved the json_encode($bar) at client.txt file
        data: [{{$bar}}], //this does not work
        dataType:"JSON",
        success: function(result){
            google.charts.load('current',{
            'packages':['corechart']
            });
            google.charts.setOnLoadCallback(function(){
            drawChart(result);
            });
        }
        });

        function drawChart(result) {server.
            var data = new google.visualization.DataTable();
            data.addColumn('string' , 'ClientName');
            data.addColumn('number', 'Price');
            var dataArray=[];
            $.each(result, function(i, obj){
            dataArray.push([obj.ClientName, parseInt(obj.Price)]);
        });
        data.addRows(dataArray);

            var barChart_options = {
            title: 'hoise??',
            is3D: 'true',
            width: 400,
            height: 300
        };
        var barChart = new google.visualization.BarChart(document.getElementById('barChart'));
                    barChart.draw(data, barChart_options);
        }
});


</script>

更新2

控制器更新

<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller {


public function index()
{

    try{
        $val=DB::connection()->getDatabaseName();
        if(DB::connection()->getDatabaseName()) {

            $barChart= DB::table('clients')
                ->select('clients.ClientName','clients.Price')
                ->get();
            $bar = json_encode($barChart);

            return view('home')
                ->with('bar', $bar);

        }else{
            $er="/connection status: database error";
            return view('errors/503')->with('error',$er);
        }
    }catch (\Exception $e){
        $er="/connection status: database error";
        return view('errors/503')->with('error',$er);
    }

}

public function barChart(){

    $barChart= DB::table('clients')
        ->select('clients.ClientName','clients.Price')
        ->get();
    echo json_encode($barChart);
}
public function create()
{
    //
}
public function store()
{
    //
}
public function show($id)
{
    //
}
public function edit($id)
{
    //
}
public function update($id)
{
    //
}
public function destroy($id)
{
    //
}
}
php laravel-5
3个回答
1
投票

将您的

clients.php
添加到
resources/views
目录并将其命名为
clients.blade.php
,它的工作原理相同,但为您提供了一个不错的模板引擎。

在您的

controller/barChart
方法中执行此操作。

$barChart= DB::table('clients')
    ->select('clients.ClientName','clients.Price')
    ->get();
return view('clients', compact('barChart'));

在视图文件

resources/views/clients.blade.php
中,您现在可以访问
$barChart
变量。

您可以在您的视图中像这样使用它:

{{ json_encode($barChart) }}

或者如果您不使用 Blade,那么

<?= json_encode($barChart) ?>

0
投票

将你的clients.php放入res

enter code here
ources/views目录并命名为clients.blade.php

像这样改变你的控制器

<?php 
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller {
public function barChart(){
$barChart=DB::table('clients')
->select('clients.ClientName','clients.Price')-  >get();
return view('clients')->with('barChart' => $barChart);
}
?>

0
投票

终于我想出了解决方案。我无法将变量传递到公共文件夹,并且在 ajax 中给出 ulr 也是一个问题,在这种情况下,创建数据表很明显,然后传递它。 控制器部分将是

public function index()
{
    try{
        $val=DB::connection()->getDatabaseName();
        if(DB::connection()->getDatabaseName()) {

            $barChart= DB::table('clients')
                ->select('clients.ClientName','clients.Price')
                ->get();

            $rows = array();
            //flag is not needed
            $flag = true;
            $table = array();
            $table['cols'] = array(
                array('label' => 'Weekly Task', 'type' => 'string'),
                array('label' => 'Percentage', 'type' => 'number')

            );

            $rows = array();
            foreach($barChart as $r) {
                $temp = array();
                $temp[] = array('v' => (String)$r->ClientName);
                $temp[] = array('v' =>(int)$r->Price);
                $rows[] = array('c' => $temp);
            }

            $table['rows'] = $rows;
            $bar = json_encode($table);





            return view('home')
                ->with('bar', $bar);

        }else{
        }
    }catch (\Exception $e){
    }

}

视图部分将是

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
                    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
                    <script type="text/javascript">

                        // Load the Visualization API and the piechart package.
                        google.load('visualization', '1', {'packages':['corechart']});

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

                        function drawChart() {

                            // Create our data table out of JSON data loaded from server.
                            var data = new google.visualization.DataTable(<?=$bar?>);
                            var options = {
                                title: 'My Chart',
                                is3D: 'true',
                                width: 400,
                                height: 300
                            };
                            // Instantiate and draw our chart, passing in some options.
                            // Do not forget to check your div ID
                            var chart = new google.visualization.BarChart(document.getElementById('barChart'));
                            chart.draw(data, options);
                        }
                    </script>

礼貌 php mysql google Chart json 完整示例

© www.soinside.com 2019 - 2024. All rights reserved.