Data Display Issues Laravel 5.1

Asked

Viewed 245 times

1

I’m a beginner in Laravel and I’m trying to bring data from a BD with multiple Postgresql schemas with Laravel 5.1 and is bringing me an error

What configuration should be done for the model to correctly access the table of that schema?

Directly by postgres would write the query this way:

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

Errorexception in Testecontroller.php line 13: Use of Undefined Registration form - assumed 'registration'

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
  • 1

    Cadastro::all() returns something? cadastro.escolaridade::all(); does not refer to class or schema. Maybe Cadastro has a property called $escolaridade.

  • Maybe that answer can help.

  • I will give my opinion by marking his question. (This error cannot be reproduced, such and such)

  • The error is not part of those files, you are getting the message, but the problem comes from elsewhere... you better figure out the problem, use Xdebug or Phpunit to test your classes. Or something simpler, like die(); var_dump() and print_r().

  • @Ivanferrer the syntax cadastro.escolaridade::all(); who is in the TesteController is invalid, it seems to me that he wants to know how to model consult a bank in one or multiple schemas.

  • By the logic of error, he is not finding the constant, give a look here.

  • Have you checked in the site of the Laravel ?

  • In PHP there is no such syntax with dot in PHP, at least not that I know: cadastro.escolaridade::all() what is there is that: {string1.string2} or this $cadastro->escolaridade::all().

  • 1

    I took the negative that I had given. But I think the question should be reworked.

  • Maybe what he wants to do is something like this:; $escolaridade = Cadastro->escolaridade::all();

Show 5 more comments

2 answers

3


According to that answer, you must define the schema in the attribute $table of your model.

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

Then you can call normally,

$escolaridade = Cadastro::all();
  • So it does. The way I did you can use both two banks, as well as databases of other connections, as well as other SQL databases. The Laravel 4 is already top, the 5 so don’t even talk. + 1. This one I didn’t know :)

  • The way @rray stated it worked!

1

That bit of code is wrong

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

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

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

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

Multiples 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 set it in model.

Example with default schema set in configuration:

namespace App\Model\Schema1;

class Pessoa {}

Exmeplo with standard schema defined in the model

namespace App\Model\Schema2;

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

Thus, you are informing the model that you will be using another connection configuration with the database for it. In this case, you use the same connection data, changing only the database.

  • 1

    The question is valid, in postgres a database can have several logical 'divisions', which are called schemas, in sql to make a query is, select * from schema.tabela or select * from cadastro.escolaridade. How to solve this problem (access to multiple schemas?

  • @rray, but it does have a syntax error. That’s what causes the error. And the Laravel accepts pg yes

  • Don’t need any extra settings? the schema where the tables are usually public but some people create others, the other day I saw that same(or almost) question, I found it curious.

  • @rray, updated. I saw what he needed. In Aravel you can use as many different connections as you want (5 for example). Just configure each one and define which models will do it. In Laravel 5, how the namespaces were implemented, I thought it was fair to do as I did in my example, putting a namespace for each schema.

  • If you understand the point of the question blz :), a schema is not a connection :P but if this is how the framework solves the problem, ball forward. I wonder what the relationship between one table of a schema and another is like through Orm :P. +1 by the answer and comment above.

  • @rray if I said anything stupid, please correct my answer :D Rsrsrsrsrsrsrs

  • @rray can make a Foreign key from one bank to the other (of course, in the same connection). I do not know if it is gambiarra :#

  • @Wallacemaxters In my BD Postgres I have 17 Schemas I don’t know if this form creating various connections would be very viable

  • So don’t do it like that. Just keep the knowledge ;). It’s better to do it like @rray

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.