Column not found: 1054 Unknown column ‘2’ in ‘on clause’ – InnerJoin query not working properly

  controller, eloquent, inner-join, laravel, sql

I am trying to execute the following code, the error applies to the $waitingToBeShipped… section of the code. As you can see I am using an innerJoin. I want to retrieve all of the rows that have orders containing products that are still waiting to be shipped.

public function destroyMany($ids) {
    $status = false;
    $ids = explode(",", $ids);
    foreach ($ids as $id) {
        $innerJoin = OrderDetails::where('product_id', $id)->pluck('order_id');
        if ($innerJoin->count()) {
            $innerJoin = preg_replace("/[^A-Za-z0-9-]/", '', $innerJoin);
        $waitingToBeShipped = Order::where('is_delivered', 0)
        ->join('order_details', '', '=', $innerJoin)

But it returns:

local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in 'on clause' (SQL: select * from `orders` inner join `order_details` on `orders`.`id` = `2` where `is_delivered` = 0 and `orders`.`deleted_at` is null)

I’ve tried to run this query manually in PHPMyAdmin, which also gives an error with the literal query that I just copied, BUT, when I remove the backticks around the number 2, it does actually give me a result.

I am not sure how I can do the same for my code? Any help would be appreciated, thanks!

Source: Laravel

Leave a Reply