Help with a simple join

2

Good night, I do not know what I'm doing wrong, I have two models, in the migration I created it so

Schema::create('endereco_tipos', function (Blueprint $table) {
         $table->increments('id');
         $table->string('nome');
         $table->timestamps();
         $table->softDeletes();
     });


Schema::create('endereco', function (Blueprint $table) {
        $table->increments('id');            
        $table->string('estado', 2)->default('PR');    
        $table->string('cidade', 300);   
        $table->string('bairro', 300);     
        $table->string('logradouro', 300);
        $table->string('logradouro_numero', 15);
        $table->string('residencia_numero', 15)->nullable();
        $table->string('complemento1', 300)->nullable();
        $table->string('complemento2', 300)->nullable();
        $table->string('responsavel', 300)->nullable();
        $table->integer('endereco_tipo_id')->unsigned();
        $table->foreign('endereco_tipo_id')->references('id')->on('endereco_tipos');
        $table->timestamps();
        $table->softDeletes();
    });

In classes I left like this

namespace teste;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use Eloquent;

class Endereco extends Eloquent
{
    use SoftDeletes;
    protected $table = 'endereco';  
    protected $dates = ['deleted_at', 'created_at', 'updated_at'];
    public function enderecoTipo(){
        return $this.hasOne(EnderecoTipo::class, 'endereco_tipo_id');
    }
}

namespace teste;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use Eloquent;

class EnderecoTipo extends Eloquent
{
    use SoftDeletes;
    protected $table = 'endereco_tipos';
    protected $dates = ['deleted_at', 'created_at', 'updated_at'];
    public function endereco(){
        return $this->belongsTo(Endereco::class);
    }
}

Inside my controller if I call so it works but does not return the type

$todos = Endereco::all();
return view("enderecos.index", ['todos' => $todos]);

I do not know how to load the type_address next to the address, I tried to do so

$todos = Endereco::with('enderecoTipo')->get();

But I had a return like this - > Call to undefined function teste\hasOne()

I tried to do this but he loaded the object with the address data mixed with the address type data

$todos = Endereco::join("endereco_tipos", "endereco.endereco_tipo_id", "=", "endereco_tipos.id")->get();

I would like a json like this to be returned to me

{ 
    id:15, 
    estado:'asdasda', 
    cidade:'asdada', 
    enderecoTipo : { id: 7, nome: 'Casa', .... } ,
    .....
}

In the view I put {{ $todos }} , so I gave a print_r and it is noticed that for some reason there is no reference to EnderecoTipo::class within Endereco::class

    
asked by anonymous 13.09.2017 / 06:20

1 answer

1

Your templates are all non-standard, they are in namespaces different from the default, and when this happens, it needs to be registered and configured so that the

13.09.2017 / 16:33