如何在雄辩的模型Laravel中动态设置外键以及如何通过计数使用它

发布于 2025-02-06 01:20:37 字数 1696 浏览 1 评论 0原文

i have a column name in a table like below

Table Code 1

34222X234X442234222X234X442334222X234X4424
A1A15A15
A1A6A7
A3A3A3
A3A3A7
A5A15A8

Table Code 2

IDcidCodeDesc
14422A1desc 1
24423A2desc 2
34422A3DESC 3
44424A4DESC 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

34222X234X442234222X234X442334222X234X4424
A1A15A15
A1A6A7
A3A3A3
A3A3A7
A5A15A8

Table Code 2

IDcidCodeDesc
14422A1desc 1
24423A2desc 2
34422A3desc 3
44424A4desc 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2025-02-13 01:20:37

您可以在模型中创建一个变量,并将其传递给它与以下相同的值:

模型:

class Code_2 extends Model
{

    protected $table = 'code_2';
    protected $field;
    public function __construct($field = 'code_2_id')
    {
        $this->field = $field;
    }

    public function unit()
    {
        return $this->hasMany(Code_1::class, $this->field, 'Code');
    }
}

和控制器您可以使用:

$column = '34222X234X4424';
$model = new Code2($column);
$detail1 = $model->withCount('unit')->get(['id', 'code', 'decs']);

You can create a variable in model and pass value for it same as :

Model :

class Code_2 extends Model
{

    protected $table = 'code_2';
    protected $field;
    public function __construct($field = 'code_2_id')
    {
        $this->field = $field;
    }

    public function unit()
    {
        return $this->hasMany(Code_1::class, $this->field, 'Code');
    }
}

And Controller you can use :

$column = '34222X234X4424';
$model = new Code2($column);
$detail1 = $model->withCount('unit')->get(['id', 'code', 'decs']);
蓝礼 2025-02-13 01:20:37

您不能使用或 中的中的参数关系,因此我建议

// Code_2 model

  public static $toCountRelations = [
    'x22',
    'x23',
    'x24'
  ];

  public function x22()
  {
    return $this->hasMany(Code_1::class, '34222X234X4422', 'Code');
  }

  public function x23()
  {
    return $this->hasMany(Code_1::class, '34222X234X4423', 'Code');
  }

  public function x24()
  {
    return $this->hasMany(Code_1::class, '34222X234X4424', 'Code');
  }

  /**
   * note that this function will return 0 if no of above relations is loaded
   *
   * @return int
   */
  public function getCodesCountAttribute()
  {
    $sum = 0;
    foreach (self::$toCountRelations as $relation)
      if ($this->relationLoaded($relation)) {
      // this syntax for $relation = 'x22' will get value of 
      // $this->x22_count
        $sum += $this->{$relation . '_count'};
      }
    return $sum;
  }

  // controller
  $detailed = Code_2::withCount(Code_2::$toCountRelations)->get(['id', 'Code', 'Desc']);  

在此步骤中定义3个关系,您收集了具有x22_count值的模型的收集/code>,x23_countx24_count和计算属性codes_count返回加载关系的全局计数,因此,如果在以后的列中列表将更改您可以简单地更改$ tocountryations带有所需关系的数组

you can't pass parametrized relations in with or withCount so i'd suggest to define 3 relations

// Code_2 model

  public static $toCountRelations = [
    'x22',
    'x23',
    'x24'
  ];

  public function x22()
  {
    return $this->hasMany(Code_1::class, '34222X234X4422', 'Code');
  }

  public function x23()
  {
    return $this->hasMany(Code_1::class, '34222X234X4423', 'Code');
  }

  public function x24()
  {
    return $this->hasMany(Code_1::class, '34222X234X4424', 'Code');
  }

  /**
   * note that this function will return 0 if no of above relations is loaded
   *
   * @return int
   */
  public function getCodesCountAttribute()
  {
    $sum = 0;
    foreach (self::$toCountRelations as $relation)
      if ($this->relationLoaded($relation)) {
      // this syntax for $relation = 'x22' will get value of 
      // $this->x22_count
        $sum += $this->{$relation . '_count'};
      }
    return $sum;
  }

  // controller
  $detailed = Code_2::withCount(Code_2::$toCountRelations)->get(['id', 'Code', 'Desc']);  

at this step you have collection of models with values of x22_count, x23_count, x24_count and computed property codes_count returning global count of loaded relations so if in future columns list will change you can simply change $toCountRelations array with required relations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文