[英]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_items和claim 。
The table claim_items has 2 columns:表claim_items有 2 列:
The table claims has many columns among which:表声明有许多列,其中:
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 来为您完成这项工作。
$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.