Error making relationship with laravel

0
  

Call to undefined method Illuminate \ Database \ Query \ Builder :: BeltTo ()

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 using Laravel 5.4

    
asked by anonymous 02.08.2017 / 20:06

2 answers

0

See if it solves the name of the field referenced in the relation, because by default Laravel identifies the fields related to the nomenclature table_id

Example:

public function tipo()
{
    return $this->belongTo(Tipo::class, 'id_tipo');
}
    
03.08.2017 / 02:58
0

Missed a "s" in of both models.

    
02.08.2017 / 22:12