0
Call to Undefined method Illuminate Database Query Builder::belongTo()
My Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Imovel extends Model
{
//definindo nome da tabela na migration
protected $table = 'imoveis';
//definindo relacao do campo id_tipo
public function tipo()
{
return $this->belongTo(Tipo::class);
}
//definindo relacao do campo id_cidade
public function cidade()
{
return $this->belongTo(Cidade::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tipo extends Model
{
protected $fillable = ['titulo'];
//definindo relaçao entre as tabelas
public function imoveis()
{
return $this->hasMany('App\Models\Imovel', 'id_tipo');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Imovel;
class Cidade extends Model
{
protected $fillable = ['nome', 'estado', 'sigla'];
//definindo relaçao entre as tabelas
public function imoveis()
{
return $this->hasMany(Imovel::class);
}
}
My controller
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Imovel;
use App\Models\Cidade;
class ImovelController extends Controller
{
public function index()
{
$imovel = new Imovel();
$imoveis = Imovel::all();
return view('admin.imoveis.index', compact('imoveis'));
}
public function adicionar()
{
$cidades = Cidade::all();
return view('admin.imoveis.adicionar', compact('cidades'));
}
}
My Vew
@extends('layouts.admin')
@section('content')
<h1>Usuarios</h1>
<table class="table table-stripped">
<tr>
<th>Id</th>
<th>Nome</th>
<th>Descrição</th>
<th>Endereçõ</th>
<th>Cidade</th>
<th>Status</th>
<th>Preço</th>
<td></td>
<th>Ações</th>
</tr>
@foreach($imoveis as $imovel)
<tr>
<td>{{ $imovel->id }}</td>
<td>{{ $imovel->titulo }}</td>
<td>{{ $imovel->descricao }}</td>
<td>{{ $imovel->endereco }}</td>
<td>{{ $imovel->cidade->nome }}</td>
<td>{{ $imovel->status }}</td>
<td>{{ number_format($imovel->valor, 2, ',', '.') }}</td>
<td>
<img src="{{ asset($imovel->imagem) }}" class="thumb">
</td>
<td>
<a href="{{ route('admin.imoveis.editar', $imovel->id)}}" class="btn btn-warning">Editar</a>
<a onclick="javascript:if(confirm('Deja excluir este usuario')){window.location.href='{{ route('admin.imoveis.deletar', $imovel->id) }}';}" class="btn btn-danger">Excluir</a>
</td>
</tr>
@endforeach
</table>
<hr>
<a href="{{ route('admin.imoveis.adicionar') }}" class="btn btn-success">Novo</a>
@endsection
I’m wearing the Laravel 5.4
Inform the version of the Standard in the question
– DNick
I’m using the 5.4.
– William
Change this line
$imoveis = Imovel::all()
for$imoveis = Imovel::with('tipo')->get()
. If this is the mistake. I will come up with an answer explaining the problem– DNick
The mistake has changed
Call to undefined relationship [tipos] on model [App\Models\Imovel].
– William
I added however the error persists in the same.
– William
Change lines
$this->belongTo('App\Models\Tipo')
and$this->belongTo('App\Models\Cidade')
in the model Imovel to$this->belongTo(Tipo::class);
and$this->belongTo(Cidade::class);
. Add the use of both.– DNick
You’re making that mistake:
Call to undefined relationship [tipos] on model [App\Models\Imovel].
again.– William
Update your model Immovel in question to see how it turned out
– DNick
I’ve updated the models.
– William
You need to add the App Models City and use App Models Type right after the namespace
– DNick
I have already looked at phpmyadmin and there is the relationship between the tables I am not able to list making a Join Index. Obs( the error remains the same).
– William
Your relations are missing configure because you did not follow the nomenclature of
Laravel
– novic