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

kevinwakhisi's avatar

ForeignIDs with different names

I don't know if this is acceptable

$table->foreignId('landlord_id')->constrained('users')->onDelete('cascade');
$table->foreignId('caretaker_id')->nullable()->constrained('users')->onDelete('cascade');

well this works when I query users with their properties the Issue is when I try to query properties and load the users/landlords

/**
     * Get the landlord that owns the Property.
     */
    public function landlord(): BelongsTo
    {
        return $this->belongsTo(User::class, 'landlord', 'id');
    }
0 likes
7 replies
Sinnbeck's avatar
return $this->belongsTo(User::class, 'landlord_id', 'id'); 
1 like
martinbean's avatar
Level 80

@kevinwakhisi Your foreign key definitions are fine, but you’re using the wrong names in your Eloquent model. Your relation methods should look something like this if you’re using non-conventional names:

public function landlord(): BelongsTo
{
    return $this->belongsTo(User::class, 'landlord_id');
}

The second argument is the name of the foreign key column. You named your foreign key column landlord_id; not just “landlord”.

1 like
kevinwakhisi's avatar

@martinbean after changing to this error shows up

    "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'properties.user_id' in 'where clause' (SQL: select count(*) as aggregate from `properties` where (`name` like %% or `slug` like %% ...

on my users model, this is what I have

/**
     * Get all of the properties for the User.
     */
    public function properties(): HasMany
    {
        return $this->hasMany(Property::class, 'landlord_id', 'id');
    }
kevinwakhisi's avatar

@Sinnbeck

public function scopeSearch($query, $term)
    {
        // code...
        $term = "%$term%";

        $query->where(function ($query) use ($term) {
            $query->where('name', 'like', $term)
            ->orWhere('slug', 'like', $term)
            ->orWhere('number_of_floors', 'like', $term)
            ->orWhere('summary', 'like', $term)
            ->orWhere('lr_no', 'like', $term)
            ->orWhere('location', 'like', $term)
            ->orWhere('city', 'like', $term)
            ->orWhere('contractor_name', 'like', $term)
            ->orWhere('contractor_email', 'like', $term)
            ->orWhereHas('landlord', function ($query) use ($term) {
                $query->where('first_name', 'like', $term)
                ->orWhere('last_name', 'like', $term)
                ->orWhere('second_name', 'like', $term)
                ->orWhere('phone', 'like', $term)
                ->orWhere('email', 'like', $term);
            })
            ->orWhereHas('caretaker', function ($query) use ($term) {
                $query->where('first_name', 'like', $term)
                ->orWhere('last_name', 'like', $term)
                ->orWhere('second_name', 'like', $term)
                ->orWhere('email', 'like', $term)
                ->orWhere('phone', 'like', $term);
            });
        });
    }
kevinwakhisi's avatar

@Sinnbeck i forgot to do the same to the caretaker (user) relationship

/**
     * Get the caretaker associated with the Property.
     */
    public function caretaker(): BelongsTo
    {
        return $this->belongsTo(User::class, 'caretaker_id');
    }

Now works fine

Please or to participate in this conversation.