gpt4 book ai didi

php - 拉拉维尔。加入后保持 Eloquent 关系

转载 作者:行者123 更新时间:2023-11-30 22:39:47 25 4
gpt4 key购买 nike


突然我遇到了我自己无法解决的问题。我写了返回一些数据的查询:

$users = User::whereIn('users.status', array(2,3))
->leftJoin('firsts', 'firsts.user_id', '=', 'users.id')
->leftJoin('seconds', 'seconds.user_id', '=', 'users.id')
->leftJoin('thirds', 'thirds.user_id', '=', 'users.id')
->leftJoin('offers', 'offers.user_id', '=', 'users.id')
->where(function($query){
$query->where('users.status', '=', '2');
$query->where('firsts.status', '=', 'approved');
$query->where('seconds.status', '=', 'approved');
$query->where('offers.user_id', '=', 'approved');
})
->orWhere(function($query){
$query->where('users.status', '=', '3');
$query->where('users.stripe_active', '=', '1');
$query->where('firsts.status', '=', 'approved');
$query->where('seconds.status', '=', 'approved');
$query->where('thirds.status', '=', 'approved');
$query->where('offers.status', '=', 'approved');
})
->get();

我期待简单的对象作为查询的返回值。目前我的用户表中有一条正确的记录,但此查询向我返回了该对象 6 次,我不明白为什么。
我期望的结果:

Collection {#353 ▼
#items: array:1 [▼
0 => User {#354 ▼
#dates: array:2 [▶]
#table: "users"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
+admin: "eval.butkus@gmail.com"
+admin2: "Evaldas"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:31 [▼
"id" => 1
"username" => "evaldas"
"password" => "$2y$10$ARTGHBTqIl/X6UsblylF5ugMPFlASwDKUEVbS5Nh3TjtycGG/NU7."
"email" => "eval.butkus@gmail.com"
"remember_token" => "A42ezMckkvjCLrpj4jxyHf2iSQWp3iAGfzGoTqMygDimFSzXNx6FW42qCOhP"
"name" => ""
"subscribe" => 0
"profile_picture" => "VNlQxOlCbXkCGEQ6ZdFS1pbK2tixhsmmhQPhLNLMBRKqDzXegWbqQiJOsplMADKg.jpg"
"about_you" => ""
"writing_service" => 0
"f_card" => 0
"logo" => ""
"website" => ""
"youtube" => ""
"status" => 3
"code" => ""
"active" => 1
"address" => ""
"phone" => ""
"news" => 0
"terms" => 1
"radius" => 0
"created_at" => "2015-07-13 10:26:47"
"updated_at" => "2015-07-15 10:25:30"
"stripe_active" => 1
"stripe_id" => "cus_6c0xE8OxR7fdXJ"
"stripe_subscription" => "sub_6c0x4RzEoBplCb"
"stripe_plan" => "small"
"last_four" => "4242"
"trial_ends_at" => null
"subscription_ends_at" => null
]
#original: array:31 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}

我得到的结果:

Collection {#364 ▼
#items: array:6 [▼
0 => User {#365 ▼
#dates: array:2 [▶]
#table: "users"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
+admin: "eval.butkus@gmail.com"
+admin2: "Evaldas"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:53 [▼
"id" => 2
"username" => "evaldas"
"password" => "$2y$10$ARTGHBTqIl/X6UsblylF5ugMPFlASwDKUEVbS5Nh3TjtycGG/NU7."
"email" => "sadsad"
"remember_token" => "A42ezMckkvjCLrpj4jxyHf2iSQWp3iAGfzGoTqMygDimFSzXNx6FW42qCOhP"
"name" => "sadsa"
"subscribe" => 0
"profile_picture" => "VNlQxOlCbXkCGEQ6ZdFS1pbK2tixhsmmhQPhLNLMBRKqDzXegWbqQiJOsplMADKg.jpg"
"about_you" => ""
"writing_service" => 0
"f_card" => 0
"logo" => ""
"website" => "das"
"youtube" => ""
"status" => "approved"
"code" => ""
"active" => 1
"address" => "asfas"
"phone" => "dasd"
"news" => 0
"terms" => 1
"radius" => 0
"created_at" => "2015-07-15 07:31:37"
"updated_at" => "2015-07-15 07:44:31"
"stripe_active" => 1
"stripe_id" => "cus_6c0xE8OxR7fdXJ"
"stripe_subscription" => "sub_6c0x4RzEoBplCb"
"stripe_plan" => "small"
"last_four" => "4242"
"trial_ends_at" => null
"subscription_ends_at" => null
"country" => "Highland"
"mobile" => "asd"
"descripsion" => "Ddescription"
"landline" => "asdas"
"company_name" => "das"
"facebook" => "dsad"
"twitter" => "sadsa"
"linkedln" => "dsadasd"
"company_number" => "sad"
"google" => "sadasd"
"vat" => "asdsad"
"experience" => ""
"privacy" => 0
"user_id" => 1
"post_code" => "AB115QN"
"latitude" => "57.14270109"
"longitude" => "-2.093014619"
"identify" => "grEa6JEaj6P0I7Zc3ZCq5CADojzApSJZ.jpg"
"bill" => "N8WKjRlSttsHNt1crrNdwxG7uqWEg5GB.jpg"
"comment" => "asdsad"
"image" => "7dmURWxbzO4pinqbek0yzdIKqEyZrjOD.jpg"
"title" => "new offer"
]
#original: array:53 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
1 => User {#366 ▶}
2 => User {#367 ▶}
3 => User {#368 ▶}
4 => User {#369 ▶}
5 => User {#370 ▶}
]
}

能解释一下我是如何丢失表之间的所有关系的吗?我该如何避免呢?由于结果的结构发生了变化,我不能使用循环、关系等。谢谢!

最佳答案

如果我没看错,你只是想

(delete join onto orders)...->groupBy('users.id')->get();

这将为您返回一个符合条件的用户。

您可以通过 2 个查询实现此目的,首先对用户进行分组(不加入订单),然后对订单进行第二次查询。要加入的其他内容取决于您使用哪些表来过滤用户,以及您使用哪些表将数据添加到订单中。在第二个查询中将数据添加到订单中,在第一个查询中填充用于过滤用户的数据。您也可以尝试子查询,但 Eloquent 支持不佳。

此外,您的 whereIn 完全没有意义,因为您已经在测试用户状态为 2 或 3 的位置。

为了更清楚一点而编辑,我有点仓促。原本以为发帖者想要后退一排 - 事实并非如此。

关于php - 拉拉维尔。加入后保持 Eloquent 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com