jeudi 26 décembre 2019

Laravel not fetching result from mongodb

I have a json file called "University.json" which contains only one row of records.

{"University": [{"fees": "$200"}, {"month": "June"}]}

Now I have used mongo import to insert the above *.json file into MongoDb.

mongoimport --db university --collection finance --file University.json 

My Mongo database/collection values are : university/finance To show the values on view blade from MongoDb , I have tried as below but not able to show the return result. Also, I have a top level element in my json file which is "University". So, is this the proper way to fetch the nested elements. Please suggest.

Route (web.php)

Route::group(['middleware' => ['auth']], function () {
     Route::get('/finance', 'FeesController@getFees');
});

Controller (FeesController.php)

...
public function getFees()
    {
        $fees = Finance::all();
        return view('tables', compact('fees'));
    }
...

Model (Finance.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;


class Finance extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'finance';

    protected $fillable = [
        'fees', 'month'
    ];
}

View blade (tables.blade.php)

<tr>
        <th>fees</th>
        <th>Month</th>
    </tr>

      @foreach($fees as $fee)
      <tr>
        <td></td>
        <td></td>
      </tr>
      @endforeach

. env file

MONGODB_CONNECTION=mongodb
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27017
MONGODB_DATABASE=university
MONGODB_USERNAME=
MONGODB_PASSWORD=

database.php

...
    'default' => env('DB_CONNECTION', 'mysql'),
        'mongodb' => [
                    'driver'   => 'mongodb',
                    'host'     => env('MONGODB_HOST', 'localhost'),
                    'port'     => env('MONGODB_PORT', 27017),
                    'database' => env('MONGODB_DATABASE', 'university'),
                    'username' => env('MONGODB_USERNAME'),
                    'password' => env('MONGODB_PASSWORD'),
                    'options'  => [
                        'database' => 'admin' // sets the authentication database required by mongo 3
                    ]
                ],

    'mysql' => [
                'driver' => 'mysql',
                'url' => env('DATABASE_URL'),
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => true,
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],
...

app.php

...
/*
    `Mongo DB
    */
    Jenssegers\Mongodb\MongodbServiceProvider::class,
...

Note : I am using both mysql and mongodb for my requirements. There is no issue while attempting simple json structure without top level element.

Edit (1) :

I have changed the controller function as :

public function getFees()
{
$fees = Finance::where(['University.month' => 'June'])->get();
   return view('tables')->with('fees', json_decode($fees, true));
}

and simply print the below in view blade shows the entire Json structure from mongo.

{!! json_encode($fees) !!}

output :

[{"_id":"5e04a06ca445f78401a16d0a","University":[{"fees":"$200"},{"month":"June"}]}]

But now how to separate the elements ? I want to show Fees = $200 and Month = June in the blade view.

I have tried but not working and giving undefined index exception fees in view.

@foreach($fees['member'] as $member)
    Fees: 
    Month: 
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire