我的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 0010Remember: The second argument of whereIn must be an array.
更多推荐
发布评论