繁体   English   中英

Illuminate\Database\Eloquent\RelationNotFoundException | 调用 model [App\Models\Property] 上的未定义关系 [neighborhood]

[英]Illuminate\Database\Eloquent\RelationNotFoundException | Call to undefined relationship [neighborhood] on model [App\Models\Property]

hasOne(地址::类); } public function city() { return $this->belongsTo(City::class); } 公共 function 目标() { 返回 $this->belongsTo(Goal::class); } public function type() { return $this->belongsTo(Type::class); } 公共 function 社区() { 返回 $this->belongsToMany(Neighborhood::class)->withTimestamps(); } public function 图片() { return $this->hasMany(Picture::class); } }

我是新来的,我的关系有些问题

<?php

命名空间应用\模型;

使用 Illuminate\Database\Eloquent\Factories\HasFactory; 使用 Illuminate\Database\Eloquent\Model;

类属性扩展模型 { 使用 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);
}

}

对于多对多关系,您必须确保已在您的情况下创建了数据透视表,您的数据透视名称表将是:property_neighborhood。 那么你必须编辑neighbors() 关系如下:

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

欢迎来到我们的社区,您误将关系名称更改为没有 s 的社区

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM