当中间模型为空时使用间接关系
use indirect relation when intermediate model is empty
我已经在我的几个模型中建立了一个模型与另一个模型的间接关系。
这是我的 Work
型号:
public function GeoEntities()
{
return $this->hasMany(\App\GeoEntity::class);
}
public function geoLand()
{
$builder = $this->GeoEntities()->where("entity_type", 0);
$relation = new HasOne($builder->getQuery(), $this, 'work_id', 'id');
return $relation;
}
public function geoLandPoints()
{
return $this->geoLand->geoPoints();
}
如果中间关系是belongsTo()并且returns是一个关系实例,
这个return $this->intermediateModel->FinalModel();
会起作用.
但在这种情况下,当 geoLand
为 Empty 时会产生错误:
Call to a member function geoPoints() on null
喜欢下面的行:
$points = $work->geoLandPoints;
- 中间关系是一个hasMany
我想要这样的关系调用 geoLandPoints
而不是 geoLandPoints()
但是,
当中间模型为空时,我想要一个空关系。
但我想不通,如何实现。
和Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin
使用 Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin
包,我尝试重构如下关系:
public function geoLandPoints()
{
$builder = $this
->select("works.*")
->join("geo_entities", "works.id", "geo_entities.work_id")
->join("geo_points", "geo_entities.id", "geo_points.geo_entity_id")
->where("entity_type", 0)
->where("works.id", $this->id);
return new HasMany($builder->getQuery(), $this, "work_id", "id");
}
但无法将 Database Query Builder 转换为 Eloquent Query Builder.
Argument 1 passed to
Illuminate\Database\Eloquent\Relations\HasOneOrMany::__construct()
must be an instance of Illuminate\Database\Eloquent\Builder, instance
of Illuminate\Database\Query\Builder given
你为什么不使用 hasOne()
方法而不是尝试 return 你自己的 HasOne
class?此外,您可以使用 withDefault()
,因此关系 return 是空的 GeoEntity
而不是 null
。
public function geoLand()
{
return $this->hasOne(\App\GeoEntity::class)->where("entity_type", 0)->withDefault();
}
您甚至可以传递一组默认值。 withDefault(['column' => 'value', 'column2' => 'value2', ...])
我已经在我的几个模型中建立了一个模型与另一个模型的间接关系。
这是我的 Work
型号:
public function GeoEntities()
{
return $this->hasMany(\App\GeoEntity::class);
}
public function geoLand()
{
$builder = $this->GeoEntities()->where("entity_type", 0);
$relation = new HasOne($builder->getQuery(), $this, 'work_id', 'id');
return $relation;
}
public function geoLandPoints()
{
return $this->geoLand->geoPoints();
}
如果中间关系是belongsTo()并且returns是一个关系实例,
这个return $this->intermediateModel->FinalModel();
会起作用.
但在这种情况下,当 geoLand
为 Empty 时会产生错误:
Call to a member function geoPoints() on null
喜欢下面的行:
$points = $work->geoLandPoints;
- 中间关系是一个hasMany
我想要这样的关系调用 geoLandPoints
而不是 geoLandPoints()
但是,
当中间模型为空时,我想要一个空关系。
但我想不通,如何实现。
和Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin
使用 Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin
包,我尝试重构如下关系:
public function geoLandPoints()
{
$builder = $this
->select("works.*")
->join("geo_entities", "works.id", "geo_entities.work_id")
->join("geo_points", "geo_entities.id", "geo_points.geo_entity_id")
->where("entity_type", 0)
->where("works.id", $this->id);
return new HasMany($builder->getQuery(), $this, "work_id", "id");
}
但无法将 Database Query Builder 转换为 Eloquent Query Builder.
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::__construct() must be an instance of Illuminate\Database\Eloquent\Builder, instance of Illuminate\Database\Query\Builder given
你为什么不使用 hasOne()
方法而不是尝试 return 你自己的 HasOne
class?此外,您可以使用 withDefault()
,因此关系 return 是空的 GeoEntity
而不是 null
。
public function geoLand()
{
return $this->hasOne(\App\GeoEntity::class)->where("entity_type", 0)->withDefault();
}
您甚至可以传递一组默认值。 withDefault(['column' => 'value', 'column2' => 'value2', ...])