如何在雄辩的模型Laravel中动态设置外键以及如何通过计数使用它
i have a column name in a table like below
Table Code 1
34222X234X4422 | 34222X234X4423 | 34222X234X4424 |
---|---|---|
A1 | A15 | A15 |
A1 | A6 | A7 |
A3 | A3 | A3 |
A3 | A3 | A7 |
A5 | A15 | A8 |
Table Code 2
ID | cid | Code | Desc |
---|---|---|---|
1 | 4422 | A1 | desc 1 |
2 | 4423 | A2 | desc 2 |
3 | 4422 | A3 | DESC 3 |
4 | 4424 | A4 | DESC 4 |
等,依此类推,
我想计算表代码1中存在多少记录,1列34222X234X44422,34222344423,3422234423,3422234x4424依赖于Code_2.cid,依赖于Code_2.cid使用Eloquent lareavel lareavel withccient code_2.cid。
我正在考虑使用模型关系一对多,但是外键不能是静态的。 是否可以将参数从控制器传递到模型关系,以及如何将其输入到计数中?
我正在尝试此事:
class Code_2 extends Model
{
public function unit($id)
{
return $this->hasMany(Code_1::class, $id, 'code);
}
}
这是我的控制器
$column = '34222X234X4424';
$detail1 = Code_2::withCount(['unit => function(Builder $query) use ($column){
$query->on('code_1.'.$column, '=', 'code_2.code');
},])->get(['id', 'code', 'decs']);
被证明是错误:“很少有参数function app \ models \ code_2 :: unit()”。
我该如何解决?
谢谢。
注意:我根本无法更改数据库。只需查看即可。
i have a column name in a table like below
Table Code 1
34222X234X4422 | 34222X234X4423 | 34222X234X4424 |
---|---|---|
A1 | A15 | A15 |
A1 | A6 | A7 |
A3 | A3 | A3 |
A3 | A3 | A7 |
A5 | A15 | A8 |
Table Code 2
ID | cid | Code | Desc |
---|---|---|---|
1 | 4422 | A1 | desc 1 |
2 | 4423 | A2 | desc 2 |
3 | 4422 | A3 | desc 3 |
4 | 4424 | A4 | desc 4 |
and so on
I want to count how many record from table Code 2 are exist in table Code 1 column 34222X234X4422, 34222X234X4423, 34222X234X4424 depend on Code_2.cid using Eloquent Laravel withCount.
I am thinking of using Model Relation one-to-many, but the foreign key cannot be static.
Is it possible to passing parameter from controller to model relationship and how to input it in the withCount?
I am trying this:
class Code_2 extends Model
{
public function unit($id)
{
return $this->hasMany(Code_1::class, $id, 'code);
}
}
here is my controller
$column = '34222X234X4424';
$detail1 = Code_2::withCount(['unit => function(Builder $query) use ($column){
$query->on('code_1.'.$column, '=', 'code_2.code');
},])->get(['id', 'code', 'decs']);
Turn out to be error "Too few arguments to function App\Models\Code_2::unit()".
How can i fix this?
Thank you.
Note: I cannot change the database at all. Just view it only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在模型中创建一个变量,并将其传递给它与以下相同的值:
模型:
和控制器您可以使用:
You can create a variable in model and pass value for it same as :
Model :
And Controller you can use :
您不能使用或 中的
中的参数关系,因此我建议
在此步骤中定义3个关系,您收集了具有
x22_count值的模型的收集/code>,
x23_count
,x24_count
和计算属性codes_count
返回加载关系的全局计数,因此,如果在以后的列中列表将更改您可以简单地更改$ tocountryations
带有所需关系的数组you can't pass parametrized relations in
with
orwithCount
so i'd suggest to define 3 relationsat this step you have collection of models with values of
x22_count
,x23_count
,x24_count
and computed propertycodes_count
returning global count of loaded relations so if in future columns list will change you can simply change$toCountRelations
array with required relations