Laravel 5模型字段爆炸以链接到其他模型(Laravel 5 model field explode to link to other models)

编程入门 行业动态 更新时间:2024-10-28 16:26:34
Laravel 5模型字段爆炸以链接到其他模型(Laravel 5 model field explode to link to other models)

我的master表中有一个字段,其中包含一个名为data的字段,其中存储的0005,0006,0010类似于0005,0006,0010 ,它们是从另一个名为table1表中提取记录的键。 它有点像belongsToMany,除了它没有分成单个记录,但功能相同。

我想知道如何从逗号中master.data字段并返回模型中table1的记录。 在这个例子中,我想知道如何返回table1.* where table1.id=0005 or 0006 or 0010 。

如果我要调用它,它将类似于auth() - > user() - > master() - > table1(),其中包含5,6,10

I have a field in my master table with a field called data which has ids stored like 0005,0006,0010 which are keys for records to be extracted from another table called table1. It's kinda like a belongsToMany except it's not broken into individual records, but same functionality.

I'm wondering how to explode master.data field off the comma and return the records for table1 in the model. In this example, I'm wondering how to return table1.* where table1.id=0005 or 0006 or 0010.

If I was to call it, it would be something like auth()->user()->master()->table1() with 5,6,10 in that array

最满意答案

你可以通过whereIn方法来做到这whereIn 。 首先,您必须从master表中获取行。 我假设行id 5有一个名为data的列包含值0005,00006,0010

$master = DB::table('master')->where('id',5)->select('data')->get(); // it returns "0005,0006,0010" $users = DB::table('table1')->whereIn('id',[$master])->get(); // it will return all rows which has id 0005, 0006 and 0010

记住: whereIn的第二个参数必须是数组。

You can do it by whereIn method. First you have to fetch the row from master table. I assume the row id 5 which has the column named data containing value 0005,00006,0010

$master = DB::table('master')->where('id',5)->select('data')->get(); // it returns "0005,0006,0010" $users = DB::table('table1')->whereIn('id',[$master])->get(); // it will return all rows which has id 0005, 0006 and 0010

Remember: The second argument of whereIn must be an array.

更多推荐

本文发布于:2023-07-21 23:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1214848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   字段   链接   Laravel   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!