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表的值。怎么了?请帮我。一世长期有这个问题。预先谢谢你:)
更改您的Bid.php模型的代码
public function area() {
return $this->belongsTo('App\Area', 'area_id','area_id');
}