Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SirMishaa's avatar

Horizon cause an invalid SQL query (Syntax error: 7 ERROR: zero-length delimited identifier at or near)

  • Horizon Version: 5.7.10
  • Laravel Version: 8.53.1
  • PHP Version: 8.0.9 (Sail)
  • Redis Driver & Version: predis/phpredis /
  • Database Driver & Version: Version shipped with PHP 8.0.9

Description:

Hello, I have a small problem.

I just discovered the job system of Laravel, so I need to trigger an HTTP request regularly from the moment the user connects to a particular page.

I installed Laravel Horizon to see what happens, but it seems that my job keeps failing, because of an SQL query that I don't understand where it comes from.

I would like to point out that my models and relationships work perfectly. It's for a client project that's due out on August 15th, so I'm a little embarrassed.

TrackProduct.php :

<?php

namespace App\Jobs;

use App\Models\ProductShop;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class TrackProduct implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private ProductShop $productShop;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(ProductShop $productShop)
    {
        $this->productShop = $productShop;
    }

    /**
     * Execute the job.
     *
     * @return string
     * @throws Exception
     */
    public function handle(): string
    {
        Log::alert('Called');
        $response = Http::get($this->productShop->url);
        $statusCode = $response->status();
        if ($statusCode !== 200 || $response->failed()) {
            throw new Exception('Unable to fetch data');
        }
        return $response->body();
    }

    /**
     * This will check to see if the job is not already present in the queue
     * @return string
     */
    public function uniqueId(): string
    {
        return $this->productShop->product_id . $this->productShop->shop_id;
    }
}

ProductShop.php :

<?php

namespace App\Models;

use App\Models\Traits\PrimaryKeyAsUUID;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProductShop extends Model
{
    use HasFactory;

    protected $primaryKey = null;
    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'shop_id',
        'product_id',
        'can_be_delivery',
        'price',
        'url',
        'is_tracked',
        'refresh_rate',
        'selector',
        'is_in_stock'
    ];

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function shop(): BelongsTo
    {
        return $this->belongsTo(Shop::class);
    }
}

ProductTrackingController.php :

<?php


namespace App\Http\Controllers;


use App\Events\BeginTrackProduct;
use App\Jobs\TrackProduct;
use App\Models\Product;
use App\Models\ProductShop;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;

class ProductTrackingController extends Controller
{
    public function trackProduct(string $slug): View
    {
        $product = Product::where('slug', $slug)->firstOrFail();
        $productShops = ProductShop::where('product_id', $product->id)->get();
        /*if ($productShops->count() > 0) {
            event(new BeginTrackProduct($product));
        }*/
        $productShops->each(fn (ProductShop $productShopRow) => $this->dispatch(new TrackProduct($productShopRow)));
        return view('stock.product', ['product' => $product, 'productShops' => $productShops]);
    }
}

Here's the stack trace error : https://gist.github.com/SirMishaa/cc87c48496f22d303edd109b491c97db

image

If you need additional information, do not hesitate. I would like to point out that I have already tried to remove the "ShoudBeUnique" but it still doesn't work. What happen? How can I fix this? Thank you in advance ❤

0 likes
2 replies
Snapey's avatar

do you have a global model scope that relies on getting something from session?

Antibilan's avatar

I was just troubleshooting similar error and found that the absence of a primary key causes such issues through "product_shops.""

protected $primaryKey = null;

In my case it was a Job failing on "order by". Haven't come up with a solution yet except adding a pk to a model.

Please or to participate in this conversation.