2
Good evening, I don’t know what I’m doing wrong, I have two models, in Migration I created like this
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 the classes I left so
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 it so it works no more returns the type
$todos = Endereco::all();
return view("enderecos.index", ['todos' => $todos]);
I don’t know how to load the type address next to the address, I tried to do so
$todos = Endereco::with('enderecoTipo')->get();
but I had such a comeback -> Call to undefined function teste\hasOne()
I tried to do so but it load 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 wish you would return me a json like this
{
id:15,
estado:'asdasda',
cidade:'asdada',
enderecoTipo : { id: 7, nome: 'Casa', .... } ,
.....
}
In the view I put like this {{ $todos }}
, then gave a print_r and it is noticed that for some reason there is no reference to the EnderecoTipo::class
within the Endereco::class
Your relationship is not configured in the model for not being a key within the convention it does not make relationships even have answer already on the site
– novic
Possible duplicate of Save Relationship 1:1 on Laravel 5.3
– novic
I looked at the link you passed and did not find the solution, for me ta giving the error Call to Undefined Function hasOne test()
– Thiago Schmitz
What would be a key within the convention?
– Thiago Schmitz
I changed there in the code of Return $this->belongsTo(Address::class); to Return $this->hasMany(Address::class); but the error continues, please help me, the link you passed me did not answer anything, my relationship is 1:N and not 1:1 I had spelled wrong, but keeps appearing Call to Undefined Function hasOne test()
– Thiago Schmitz
If the relationship is 1:N I have also answered: How to use hasmany relationship in Laravel 5.2?, your model is pulling the namespace wrong and it’s not heritage with Eloquent it’s with Model, Observer all this!
– novic