如何在 laravel 中应用与两个 ID 的关系
How to apply relationship with two id's in laravel
所以我有一个模型叫做 data_storage
和另一个模型 entity_states
我必须使用 entity_states
从 data_storage
获取记录,其中 entity_state 有 data_storage_id
和 state_id
。
如何使用 eloquent 来实现这个目标?
或者我必须使用查询生成器并使用 innerJoin?
更新1
我的实际查询
$this->values['new_leads'] = $data_storages->with('actions','states','sla')->where('wf_id',$wfid)->get();
我的data_storage
模态
class data_storages extends Model
{
//
protected $fillable = ['layout_id','member_id','company_id','team_id','data','status','wf_id'];
function actions()
{
return $this->hasMany('App\Models\ActionDataMaps', 'data_id', 'id' );
}
function states()
{
return $this->hasOne('App\Models\workflow_states','id','status');
}
function sla()
{
//Here I have to get those row from entity_states model where , data_storage_id and state_id
}
}
谢谢
对于查询生成器,您可以使用这个:
DB::table('data_storage')
->join('entity_states','data_storage.data_storage_id','=','entity_states.state_id')
->get();
在您的模型中 data_storage
您可以定义一个 属性 / 方法 entity_states
来获取它们:
class data_storage extends Model
{
public function entity_states()
{
return $this->hasMany('App\entity_states','data_storage_id')->where('state_id ','=',$this->table());
}
}
然后你可以通过
在一个实例中访问它们
$entityStatesOfDataStorage = $yourDataStorageInstance->entity_states;
看到这个link:
https://laravel.com/docs/5.3/eloquent-relationships
更合理的做法是:
class DataStorage extends Model {
public states() {
return $this->belongsToMany(State::class,"entity_states");
}
}
class State extends Model {
public storages() {
return $this->belongsToMany(DataStorage::class,"entity_states");
}
}
然后您可以通过以下方式预加载相关模型:
$storage = DataStorage::with("states")->first();
$storage->states->first()->column_in_related_state;
或通过州:
$state = State::with("storages")->first();
$state->storages->first()->column_in_related_storage;
如果 数据透视表 table entity_states
中还有其他列,那么您可以在关系中引用它们,例如:
public states() {
return $this->belongsToMany(State::class)->withPivot("pivot_column");
}
所以我有一个模型叫做 data_storage
和另一个模型 entity_states
我必须使用 entity_states
从 data_storage
获取记录,其中 entity_state 有 data_storage_id
和 state_id
。
如何使用 eloquent 来实现这个目标?
或者我必须使用查询生成器并使用 innerJoin?
更新1
我的实际查询
$this->values['new_leads'] = $data_storages->with('actions','states','sla')->where('wf_id',$wfid)->get();
我的data_storage
模态
class data_storages extends Model
{
//
protected $fillable = ['layout_id','member_id','company_id','team_id','data','status','wf_id'];
function actions()
{
return $this->hasMany('App\Models\ActionDataMaps', 'data_id', 'id' );
}
function states()
{
return $this->hasOne('App\Models\workflow_states','id','status');
}
function sla()
{
//Here I have to get those row from entity_states model where , data_storage_id and state_id
}
}
谢谢
对于查询生成器,您可以使用这个:
DB::table('data_storage')
->join('entity_states','data_storage.data_storage_id','=','entity_states.state_id')
->get();
在您的模型中 data_storage
您可以定义一个 属性 / 方法 entity_states
来获取它们:
class data_storage extends Model
{
public function entity_states()
{
return $this->hasMany('App\entity_states','data_storage_id')->where('state_id ','=',$this->table());
}
}
然后你可以通过
在一个实例中访问它们$entityStatesOfDataStorage = $yourDataStorageInstance->entity_states;
看到这个link: https://laravel.com/docs/5.3/eloquent-relationships
更合理的做法是:
class DataStorage extends Model {
public states() {
return $this->belongsToMany(State::class,"entity_states");
}
}
class State extends Model {
public storages() {
return $this->belongsToMany(DataStorage::class,"entity_states");
}
}
然后您可以通过以下方式预加载相关模型:
$storage = DataStorage::with("states")->first();
$storage->states->first()->column_in_related_state;
或通过州:
$state = State::with("storages")->first();
$state->storages->first()->column_in_related_storage;
如果 数据透视表 table entity_states
中还有其他列,那么您可以在关系中引用它们,例如:
public states() {
return $this->belongsToMany(State::class)->withPivot("pivot_column");
}