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

maltekiefer's avatar

Query a relationsship

I am trying to query a releationship like this:

        $first = Projectvoucheritem::select('*','rentalstop AS tdate')
            ->with('transmissionbegin')
            ->with('transmissionend')
            ->with('transmissiondrivers.users')
            ->whereNotNull('rentalstop')
            ->whereDate('rentalstop', '>=', $start)
            ->whereDate('rentalstop', '<=', $end)
            ->with(['projectvoucher.projectvouchertype' => function ($query) {
                $query->where('internalname',  \App\Models\Projectvouchertype::NAME_RESERVATION)->orWhere('projectvoucher.projectvouchertype.internalname', \App\Models\Projectvouchertype::NAME_ORDER);
            }]);

But when I try it like this, I get this error:

SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "projectvouchertype" LINE 1: ...hertypes"."id" in () and "internalname" =  or "projectvo... ^ (SQL: select * from "projectvouchertypes" where ("projectvouchertypes"."id" in (4) and "internalname" = reservation or "projectvoucher"."projectvouchertype"."internalname" = order) and "projectvouchertypes"."deleted_at" is null)

What I am missing here?

0 likes
3 replies
Turcinovic's avatar

It seems that the table with the name "projectvouchertype" was not found.

Maybe you have a typo?

MichalOravec's avatar

Pleaseexplaintomewhyyounameeverythinginyourprojectlikethissentence.

maltekiefer's avatar

It is a project that I have taken over and was not developed by me. I should only extend it accordingly and there is no time and money from the customer to change everything accordingly.

@turcinovic Unfortunately no, I have checked it accordingly.

In my model for Projectvoucher I have the following relationships:

	public function projectvouchertype() {
		return $this->belongsTo('\App\Models\Projectvouchertype');
	}
	public function projectvoucheritems() {
		return $this->hasMany('\App\Models\Projectvoucheritem', 'projectvoucher_id', 'id')->orderBy('linenum');
	}

Please or to participate in this conversation.