多个数据库 laravel

Multiple database laravel

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

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']}}

输出是: enter image description here

我只想显示 10000。

你应该试试这个:

您的控制器

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

不要忘记更改 $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));
    }