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

时间:2016-05-05 04:44:39

标签: php laravel-5

我想将控制器中的值传递给公共文件夹中的.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)
{
    //
}
}

3 个答案:

答案 0 :(得分: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) }}

或者如果您不使用刀片,那么

<?= json_encode($barChart) ?>

希望这有帮助。

答案 1 :(得分: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);
}
?>

答案 2 :(得分:0)

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

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 complete example