以上代码返回错误,如下Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cabinetOrderId' in 'where clause' (Connection: mysql, SQL: select * from `order_box_details` where exists (select * from `order_details` where `order_box_details`.`orderBoxId` = `order_details`.`id` and `orderId` in (select `id` from `order_details` where `cabinetOrderId` = 10))) in file
时间: 2023-08-16 17:09:54 浏览: 215
非常抱歉,我之前的回答有误。根据您提供的表结构,我们需要进行以下修正:
在 `CabinetOrderDetail` 模型中,将关联方法名从 `inboundOrderDetails()` 更改为 `orderDetails()`:
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CabinetOrderDetail extends Model
{
protected $table = 'cabinet_order_details';
protected $primaryKey = 'id';
public function orderDetails()
{
return $this->hasMany(OrderDetail::class, 'cabinetOrderId', 'id');
}
}
```
接下来,更新查询代码:
```php
$cabinetOrderId = 10; // 替换为实际的 cabinetOrderId 值
$orderBoxDetails = OrderBoxDetail::whereHas('orderDetail', function ($query) use ($cabinetOrderId) {
$query->whereIn('orderId', function ($query) use ($cabinetOrderId) {
$query->select('id')
->from('order_details')
->whereIn('cabinetOrderId', function ($query) use ($cabinetOrderId) {
$query->select('id')
->from('cabinet_order_details')
->where('cabinetOrderId', $cabinetOrderId);
});
});
})->get();
foreach ($orderBoxDetails as $orderBoxDetail) {
echo $orderBoxDetail->name;
}
```
这样,我们就可以通过 `CabinetOrderDetail` 模型的 `orderDetails` 关联关系链查询到符合条件的 `order_box_details` 表的 `name` 字段。
非常抱歉给您带来的困扰,希望这次的回答能够帮助您解决问题。如有任何疑问,请随时向我提问。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)