Error while doing relationship with Lockable

Asked

Viewed 649 times

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

  • I’m using the 5.4.

  • 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

  • The mistake has changed Call to undefined relationship [tipos] on model [App\Models\Imovel].

  • I added however the error persists in the same.

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

  • You’re making that mistake: Call to undefined relationship [tipos] on model [App\Models\Imovel]. again.

  • Update your model Immovel in question to see how it turned out

  • I’ve updated the models.

  • You need to add the App Models City and use App Models Type right after the namespace

  • 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).

  • Your relations are missing configure because you did not follow the nomenclature of Laravel

Show 7 more comments

2 answers

0

Lacked a "s" in the belongsTo of both models.

  • You did it in hand because relationships are missing set up!

  • What point in the model should I arrange for relationships to be automatic?

  • put in your question all tables and their relations I give a total example!

0


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

Example:

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

Browser other questions tagged

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