Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 与自身条件的雄辩关系_Laravel_Eloquent_Laravel Eloquent - Fatal编程技术网

Laravel 与自身条件的雄辩关系

Laravel 与自身条件的雄辩关系,laravel,eloquent,laravel-eloquent,Laravel,Eloquent,Laravel Eloquent,我对呼叫关系本身有问题 这是我的计划 table bank |id|cash_id|type table cash_small |id|title| table cash_big |id|title| 我需要根据表bank的类型调用关系的动态条件 public function small(){ return $this->belongsTo('App\CashSmall', 'cash_id')->where('type','small'); } public func

我对呼叫关系本身有问题

这是我的计划

table bank
|id|cash_id|type

table cash_small
|id|title|

table cash_big
|id|title|
我需要根据表bank的类型调用关系的动态条件

public function small(){
  return $this->belongsTo('App\CashSmall', 'cash_id')->where('type','small');
}

public function big(){
  return $this->belongsTo('App\CashSmall', 'cash_id')->where('type','big');
}
但结果是,当条件在关系表上工作时,不在银行表中

你能帮我解决这个问题吗


Thank的

不清楚为什么您有两个不同的表来存放
小额现金
大额现金
。有一个名为
cash
的表和一个名为
type
的字段。然后可以使用laravel局部范围

因此,在您的模型
Bank.php
中,您将拥有一个名为cash的关系

public function cash() 
{
  return $this->belongsTo('App\Cash', 'cash_id');
}
现在,在
Cash.php
中,您可以创建这样一个范围

public function scopeSize($query, $type = 'small') //can be whatever you want
{
    return $query->where('type', $type);
}
现在,您所要做的就是在需要时添加查询范围,例如
$bank->cash()->size('small')

现在,您的
cash()
方法将返回所有的cash,并添加范围以过滤您想要的结果


更多信息请查看laravel范围文档

听起来您实际上想要使用多态关系。表银行可以有多个
cash\u small
cash\u big
?我的意思是,银行与cash\u small或cash\u big有一对多的关系?@himanshhuupadhyay是的cash\u id可以根据类型用于cash\u small或cash\u big