简体   繁体   English

Laravel:如何查询表,从每个唯一 item_id 的列中获取第一个值并将其分配给另一个表列

[英]Laravel: how to query a table, take the first value of from a column for each unique item_id and assign it to another table column

I am completely new to Laravel and migrations in general.我对 Laravel 和一般迁移完全陌生。

I have 2 tables: claim_items and claims .我有 2 个表: claim_itemsclaim

The table claim_items has 2 columns:claim_items有 2 列:

  1. claim_id and refers to the id of the table claim (1 claim might have 1 or more claim items, while 1 or multiple claim items have only 1 claim) claim_id并指表声明的 id(1 个声明可能有 1 个或多个声明项,而 1 个或多个声明项只有 1 个声明)
  2. claim_type .声明类型

The table claims has many columns among which:声明有许多列,其中:

  1. id (of course)身份证(当然)
  2. claim_items_type claim_items_type

I need to write a migration that will query claim_items table, and for each claim_id (for each claim) it takes the first value of claim_type and assign that value to the column claim_items_type in the table claims (so that each claim has a column claim_items_type with the first value found from claim_items table relative to correspondent claim_id).我需要编写一个迁移来查询 claim_items 表,并且对于每个 claim_id (对于每个声明),它采用 claim_type 的第一个值并将该值分配给表声明中的列 claim_items_type (以便每个声明都有一个列 claim_items_type 与从 claim_items 表中找到的第一个值,相对于对应的 claim_id)。

I tried lots of variation and I ended up messing everything and confusing myself more than before.我尝试了很多变化,最终我把一切都弄乱了,比以前更让自己困惑。

Any idea on how could I achieve this?关于如何实现这一目标的任何想法?

As Gert B explained, this looks like a custom command and you should be putting this under your controller.正如Gert B解释的那样,这看起来像一个自定义命令,您应该将它放在 controller 下。 The migration file would simply contain create table and drop table commands in laravel language and is useful only for creating database table structure.迁移文件将仅包含 laravel 语言的创建表和删除表命令,并且仅对创建数据库表结构有用。 There is absolutely nothing with respect to data in it.里面绝对没有数据。 Further, if you want to insert/update the database with custom data, you should either go for seeder (that would update something like test data in your database) or you should be writing your custom controller which would do this job for you.此外,如果您想使用自定义数据插入/更新数据库,您应该使用 go 进行播种(这将更新数据库中的测试数据之类的内容),或者您应该编写自定义 controller 来为您完成这项工作。

Suggestion would be建议是

  • Update your claim_items using direct SQL insert or migration file.使用直接 SQL 插入或迁移文件更新您的 claim_items。
  • Write a custom controller and user first() to grab the first occurrence of the result.编写自定义 controller 和用户 first() 以获取结果的第一次出现。
$conditions['column_name'] = 'Your required value';
$answer = DB::table('claim-item')->where($conditions)->first();

You may use this under a loop to get closer to your requirement.您可以在循环下使用它来更接近您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将每列中的 2 个值输入到另一个表中的一列 - how to input 2 value from each column to one column in another table PHP将第一个表列ID添加到另一个表列ID - PHP add first table column id to another table column id MySQL从另一个表分配一个唯一值 - MySQL assign a unique value from another table Laravel验证从一列获取值并将其与另一个表列进行比较 - Laravel validation get value from a column and compare it to another table column 如何根据 Laravel 8 中同一表中的另一列在列中存储值 - How to store value in column depending on another column in same table in Laravel 8 查询列在另一个表中的位置 - laravel - Query where column is in another table - laravel Codeigniter查询:当我使用sum Select函数从另一个表中获取数据时,如何从表中的列中获取所有记录 - codeigniter query: how to take all the record from a column in a table when i use sum Select function to get data from another table Laravel - 从关系查询数据透视表中的列值 - Laravel - query for column value in pivot table from relationship Laravel 查询检查表列值是否存在 - Laravel query check if table column value exists 如何通过 Laravel 中的外键 ID 从表列的不同单元格返回值 8.* - How to return value from different cell of table column by foreign key id in Laravel 8.*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM