namespace App;
use App\Model\Service\Area;
use App\Model\Bid\Service;
use Illuminate\Database\Eloquent\Model;
class Bid extends Model
{
protected $table = "bid";
protected $primaryKey = 'bid_id';
protected $guarded = [];
protected $with = ['services'];
public function services() {
return $this->hasMany(Service::class, 'bid_id');
}
public function area() {
return $this->belongsTo(Area::class, 'area_id', 'area_id');
}
}
namespace App\Model\Service;
use Illuminate\Database\Eloquent\Model;
class Area extends Model
{
protected $table = "location_area";
protected $primaryKey = 'area_id';
protected $guarded = [];
public function city()
{
return $this->belongsTo(City::class, 'city_id');
}
}
当我尝试访问时
Bid::with('area')->find(BID_ID);
返回Null 查询触发错误:
"select * from `location_area` where `location_area`.`area_id` in (0)"
但是,如果我这样做:
$bid = Bid::find(BID_ID);
dd($bid->area);
它返回Area表值。怎么了?请帮我。一世 长期有这个问题。预先谢谢你:)
答案 0 :(得分:1)
必须在 MID模型
中将您声明为方法 public function area()
{
return $this->belongsTo(Area::class, 'bid');
}
像这样的东西
之后,您访问with()
Bid::with('area')->find(BID_ID);
答案 1 :(得分:0)
在您的Bid模型中更改此功能:
public function area() {
return $this->belongsTo(Area::class, 'area_id');
}