多个数据库laravel

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

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

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。

laravel show laravel-blade multi-database
2个回答
-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

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));
    }
© www.soinside.com 2019 - 2024. All rights reserved.