Problems viewing the Data Laravel 5.1 [closed]

1

I'm a beginner in Laravel and I'm trying to get data from a DB with multiple Postgresql schemas with Laravel 5.1 and it's bringing me an error

What configuration should I make for the model to correctly access the table of that schema?

Directly from postgres would write the query this way:

 SELECT * FROM cadastro.escolaridade  
 SELECT * FROM schema.tabela
  

ErrorException in TestController.php line 13: Use of undefined   constant cadastre - assumed 'cadastre'

Database.php

'pgsql' => [
'driver'   => 'pgsql',
'host'     => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset'  => 'utf8',
'prefix'   => '',
'schema'   => 'public',
],

Model

<?php


namespace App\Models\Programas;

use Illuminate\Database\Eloquent\Model;

class Cadastro extends Model
{

}

Controller

<?php
 namespace App\Http\Controllers;
 use App\Models\Programas\Cadastro;


 class TesteController extends Controller
 {


public function getIndex()
{
    $escolaridade = cadastro.escolaridade::all();


    return view('programas.escrituras.index', compact('escolaridade'));


}

View

@forelse($escolaridade as $descricao)

<p><b>Grau de Escolaridade:</b>{{$descricao->descricao}}</p>

@empty
    <p>Nenhum Grau Cadastrado!</p>

    @endforelse
    
asked by anonymous 04.01.2016 / 20:41

2 answers

3

According to this response , you should set the schema in the $table attribute of your model.

class Cadastro extends Model
{
    protected $table = 'cadastro.escolaridade';
}

Then you can call normally,

$escolaridade = Cadastro::all();
    
05.01.2016 / 12:07
1

This snippet of code is wrong

$escolaridade = cadastro.escolaridade::all();

Do you come from another language and are you trying to access members of classes or objects?

Your code indicates that you are trying to concatenate a cadastro constant with the result brought by escolaridade::all() (which instead is converted to Json , since it is concatenating). Since the constant does not exist, the error is generated.

This is how PHP understands your code. So if it is not the desired result, you have to change it.

Multiple Schemas

In Laravel you could use more than one connection to solve this problem.

For example, you can set your configuration like this:

return [

    'default' => 'pgsql',

    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', '127.0.0.1'),
        'database' => 'schema_1',
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],

    'pgsql_2' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', '127.0.0.1'),
        'database' => 'schema_2',
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ]
];

And in the model that wants to use another schema (different from what is in default ), you can define it in the model.

Example with default schema set in configuration:

namespace App\Model\Schema1;

class Pessoa {}

Example with default schema defined in the model

namespace App\Model\Schema2;

class TabelaQualquer
{
       protected $connection = 'pgsql_2';
}

So you are telling the model that you will be using another database connection configuration for it. In this case, you use the same connection data, just changing the database.

    
04.01.2016 / 20:53