0

如何在视图中显示来自其他数据库的值?我正在使用多数据库。我的控制器:

public function index()
    {

        $oil = DB::connection('mysql2')->table('oils')->select('oil_price')->get();

        $this->oil = [
            'oil_price' => $oil
        ];

        return view('home')->with('oil', $this->oil);
    }

这是我的观点:

 {{$oil['oil_price']}}

输出为: 在此处输入图像描述

我只想显示 10000。

4

2 回答 2

0

不要忘记更改$mySqlConnection, $tableName, $filedName

public function index()
{

    //to get all the value of oil_price

    $mySqlConnection = 'CONNECTION_NAME';
    $tableName = 'TABLE_NAME';
    $filedName = 'FILED_NAME';

    //to get all the oil_price(only) from the databse

     $controlVariableOne = DB::connection($mySqlConnection)->table($tableName)
                                            ->select($filedName)
                                            ->get();

                                            foreach ($controlVariableOne as $controlVariableOneKey => $controlVariableOneValue) 
                                            {
                                                $allcontrolVariableOneValues [] = $controlVariableOneValue->$filedName;

                                            }

        $controlVariableTwo = DB::connection($mySqlConnection)->table($tableName)
                                            ->select($filedName)
                                            ->first()->$filedName;



                        dd('All the Values From Databse ', $allcontrolVariableOneValues, 'First From Databse ',$controlVariableTwo);

        $viewShare = ['controlVariableOne','controlVariableTwo'];

        return view('home', compact($viewShare));
    }
于 2019-02-14T11:13:04.960 回答
-1

你应该试试这个:

你的控制器

public function index()
    {

        $oil = DB::connection('mysql2')->table('oils')->select('oil_price')->get();



        return view('home',compact('oil'));
    }

您的观点如下

@foreach($oil as $oildetails)

 {{$oildetails->oil_price}}

@endforeach
于 2019-02-14T07:41:12.297 回答