具有多个 where 条件的 Laravel 模型获取不清楚的数据
大家好我想问一下我的问题。对于消息应用程序,我想获取列 a 为 b 或列 b 为 a 时的数据。我想你看到代码就明白了。而且我也认为我写代码没有做错。 响应
这是我的代码和此测试的
var uid: "QqAIR5Qwd6erFQIgSG"
var friend_uid: "Pk6R56XmWMywcObV3a"
变量测试 1
Chat::where(['sender'=>$request->uid, 'receiver'=>$request->friend_uid])->get();
响应
"data": [
{
"id": 4,
"sender": "QqAIR5Qwd6erFQIgSG",
"receiver": "Pk6R56XmWMywcObV3a",
"message": "eyJpdiI6Ik8xSld4QlQ3bmMxaWxBMVlua3B0WXc9PSIsInZhbHVlIjoidGMvNGpXVmowVDczRUhSdGM0UkFQNHNjSVVZcFEyeFJlRThWNGdhMWZQMUFodkIwNWJtNmg4cmg1T2NOTm5ZdXBkdzdWWWhUY3hDQUtnVFBGcHllMkE9PSIsIm1hYyI6IjRkYTI5OTk3ZTRhMDAwZTdkYzgwODVmOTgyYzdlYTY2NDg0ZGM2YmExMDVkYzdiMzRhOTliYjA2MjU1ZTc3ZTkiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
}
]
这是有效的响应
测试 2
Chat::where(['sender'=>$request->friend_uid, 'receiver'=>$request->uid])->get();
响应
"data": []
这是有效是因为它在数据库中不存在,如果存在那么他会出现我已经尝试过了。
测试3(我想这样做)
Chat::where(['sender'=>$request->uid, 'receiver'=>$request->friend_uid])->orWhere('sender'=>$request->friend_uid, 'receiver'=>$request->uid)->get();
响应
"data": [
{
"id": 2,
"sender": "30B67S1tV7O7sJRG2l",
"receiver": "QqAIR5Qwd6erFQIgSG",
"message": "eyJpdiI6IkFDaFNMNjZESk96ZU5rSTQyMVhOUEE9PSIsInZhbHVlIjoiaDZyZU9TRSs2c0VTdkVlU2ZDd2JvZHpvQTh3KzZIYjk0NWJrbm90Tk9WREhBV2lrTS9xQW9KOXIza0MwaHUyMGUrTnlBS21aNXBRd1BweXJldlhiOWkzR0hqeHlFWERpdXczZ1E4NTJCSUU9IiwibWFjIjoiMzA3YmQwZmM4NjFlOTk3YmQwNzMwMzgxMTQ4ZmRhZDBhY2IzOWJkZWMzYWVhZmQyNjQ2NGI1MzM4NDQ2ZWY4YiIsInRhZyI6IiJ9",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
},
{
"id": 4,
"sender": "QqAIR5Qwd6erFQIgSG",
"receiver": "Pk6R56XmWMywcObV3a",
"message": "eyJpdiI6Ik8xSld4QlQ3bmMxaWxBMVlua3B0WXc9PSIsInZhbHVlIjoidGMvNGpXVmowVDczRUhSdGM0UkFQNHNjSVVZcFEyeFJlRThWNGdhMWZQMUFodkIwNWJtNmg4cmg1T2NOTm5ZdXBkdzdWWWhUY3hDQUtnVFBGcHllMkE9PSIsIm1hYyI6IjRkYTI5OTk3ZTRhMDAwZTdkYzgwODVmOTgyYzdlYTY2NDg0ZGM2YmExMDVkYzdiMzRhOTliYjA2MjU1ZTc3ZTkiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
},
{
"id": 87,
"sender": "chT9eSJ0N6HAXlkUUL",
"receiver": "QqAIR5Qwd6erFQIgSG",
"message": "eyJpdiI6Ikd4ZFZZTE1IVXRGQXNCVmUxUmJhcUE9PSIsInZhbHVlIjoiNWNUZE91Mk41K2o5Si9TSktOdEpNVSswYlVXZDlQd2hyTWY0d256cEJ4YkZKNVg0Y2hwbmNiUTJkZFhMSGxzVSIsIm1hYyI6IjA5ZDk2MDNkYzFhYzY0MTNlNTM3ODNmZDFiZDU3OTc2NThkODg4MGQzZGM5ZWQyYWRjM2Q1YzJmODkwNmUwOTYiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
}
]
为什么当我尝试使用这样的查询时,我得到的数据与uid或friend_uid不匹配?发生了什么事,如果我的程序错误在哪里?请帮忙,谢谢。
通过运行 php artisan migrate:refresh 解决
Hello everyone i want to ask about my problem. I want to get which data if column a is b or column b is a for a messaging app. I think you will understand when you see the code. And I also think I did nothing wrong in writing the code. this is my code and the response
Variable for this test
var uid: "QqAIR5Qwd6erFQIgSG"
var friend_uid: "Pk6R56XmWMywcObV3a"
Test 1
Chat::where(['sender'=>$request->uid, 'receiver'=>$request->friend_uid])->get();
Response
"data": [
{
"id": 4,
"sender": "QqAIR5Qwd6erFQIgSG",
"receiver": "Pk6R56XmWMywcObV3a",
"message": "eyJpdiI6Ik8xSld4QlQ3bmMxaWxBMVlua3B0WXc9PSIsInZhbHVlIjoidGMvNGpXVmowVDczRUhSdGM0UkFQNHNjSVVZcFEyeFJlRThWNGdhMWZQMUFodkIwNWJtNmg4cmg1T2NOTm5ZdXBkdzdWWWhUY3hDQUtnVFBGcHllMkE9PSIsIm1hYyI6IjRkYTI5OTk3ZTRhMDAwZTdkYzgwODVmOTgyYzdlYTY2NDg0ZGM2YmExMDVkYzdiMzRhOTliYjA2MjU1ZTc3ZTkiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
}
]
This is valid response
Test 2
Chat::where(['sender'=>$request->friend_uid, 'receiver'=>$request->uid])->get();
Response
"data": []
This is valid because it doesn't exist in the database, if there is then he will appear I've tried it.
Test 3 (i want to do this)
Chat::where(['sender'=>$request->uid, 'receiver'=>$request->friend_uid])->orWhere('sender'=>$request->friend_uid, 'receiver'=>$request->uid)->get();
Response
"data": [
{
"id": 2,
"sender": "30B67S1tV7O7sJRG2l",
"receiver": "QqAIR5Qwd6erFQIgSG",
"message": "eyJpdiI6IkFDaFNMNjZESk96ZU5rSTQyMVhOUEE9PSIsInZhbHVlIjoiaDZyZU9TRSs2c0VTdkVlU2ZDd2JvZHpvQTh3KzZIYjk0NWJrbm90Tk9WREhBV2lrTS9xQW9KOXIza0MwaHUyMGUrTnlBS21aNXBRd1BweXJldlhiOWkzR0hqeHlFWERpdXczZ1E4NTJCSUU9IiwibWFjIjoiMzA3YmQwZmM4NjFlOTk3YmQwNzMwMzgxMTQ4ZmRhZDBhY2IzOWJkZWMzYWVhZmQyNjQ2NGI1MzM4NDQ2ZWY4YiIsInRhZyI6IiJ9",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
},
{
"id": 4,
"sender": "QqAIR5Qwd6erFQIgSG",
"receiver": "Pk6R56XmWMywcObV3a",
"message": "eyJpdiI6Ik8xSld4QlQ3bmMxaWxBMVlua3B0WXc9PSIsInZhbHVlIjoidGMvNGpXVmowVDczRUhSdGM0UkFQNHNjSVVZcFEyeFJlRThWNGdhMWZQMUFodkIwNWJtNmg4cmg1T2NOTm5ZdXBkdzdWWWhUY3hDQUtnVFBGcHllMkE9PSIsIm1hYyI6IjRkYTI5OTk3ZTRhMDAwZTdkYzgwODVmOTgyYzdlYTY2NDg0ZGM2YmExMDVkYzdiMzRhOTliYjA2MjU1ZTc3ZTkiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
},
{
"id": 87,
"sender": "chT9eSJ0N6HAXlkUUL",
"receiver": "QqAIR5Qwd6erFQIgSG",
"message": "eyJpdiI6Ikd4ZFZZTE1IVXRGQXNCVmUxUmJhcUE9PSIsInZhbHVlIjoiNWNUZE91Mk41K2o5Si9TSktOdEpNVSswYlVXZDlQd2hyTWY0d256cEJ4YkZKNVg0Y2hwbmNiUTJkZFhMSGxzVSIsIm1hYyI6IjA5ZDk2MDNkYzFhYzY0MTNlNTM3ODNmZDFiZDU3OTc2NThkODg4MGQzZGM5ZWQyYWRjM2Q1YzJmODkwNmUwOTYiLCJ0YWciOiIifQ==",
"created_at": "2022-03-02T09:29:44.000000Z",
"updated_at": "2022-03-02T09:29:44.000000Z"
}
]
why when I try with a query like this I get data which doesn't match the uid or friend_uid? what happened and if where is the error in my program?, please help, thank you.
SOLVED BY RUN php artisan migrate:refresh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪。我在您的代码中看不到任何问题。如果您找不到修复程序并且只想使用快速解决方案来解决它,只需合并您创建的两个工作集合:
我知道,不是很干净,但如果您无法解决问题,这是一个不错的解决方案问题要从根源上解决。
That is very odd. I could not see any issues in your code. If you can't find a fix and just want to get it over with using a quick solution, simply merge the two working collections you have created:
Not very clean, I know, but it's an okay solution if you can't solve the problem from its root.