0

How to show the value from other database in a view? i am using multi database. My Controller :

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);
    }

This is my view :

 {{$oil['oil_price']}}

Output is : enter image description here

I want show only 10000.

2 Answers2

0

Don't forget to change the $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));
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
-1

You should try this:

Your Controller

public function index()
    {

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



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

Your view like :

@foreach($oil as $oildetails)

 {{$oildetails->oil_price}}

@endforeach
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57