Home > Blockchain >  Illuminate\Database\Eloquent\RelationNotFoundException | Call to undefined relationship [neighbor
Illuminate\Database\Eloquent\RelationNotFoundException | Call to undefined relationship [neighbor

Time:07-24

hasOne(Address::class); } public function city() { return $this->belongsTo(City::class); } public function goal() { return $this->belongsTo(Goal::class); } public function type() { return $this->belongsTo(Type::class); } public function neighborhoods() { return $this->belongsToMany(Neighborhood::class)->withTimestamps(); } public function pictures() { return $this->hasMany(Picture::class); } }

CodePudding user response:

i'm new and i have some trouble with a relation

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class Property extends Model { use HasFactory;

protected $fillable = [
    'title',
    'terreno',
    'salas',
    'banheiros',
    'dormitorios',
    'garagens',
    'descricao',
    'preco',
    'city_id',
    'type_id',
    'goal_id'
];

public function address()
{
    return $this->hasOne(Address::class);
}

public function city()
{
    return $this->belongsTo(City::class);
}

public function goal()
{
    return $this->belongsTo(Goal::class);
}

public function type()
{
    return $this->belongsTo(Type::class);
}

public function neighborhoods()
{
    return $this->belongsToMany(Neighborhood::class)->withTimestamps();
}

public function pictures()
{
    return $this->hasMany(Picture::class);
}

}

CodePudding user response:

For a Many To Many relationship you have to make sure that you have already created the pivot table in your case your pivot name table would be: property_neighborhood. then you have to edit the neighborhoods() relationship as following:

public function neighborhoods()
{
    return $this->belongsToMany(Neighborhood::class , 'property_neighborhood', 'neighborhood_id', 'property_id')->withTimestamps();
}

CodePudding user response:

Welcome to our community you have mistaken with the name of the relation change neighborhoods to neighborhood without s

public function neighborhood()
{
    return $this->belongsToMany(Neighborhood::class)->withTimestamps();
}
  • Related