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

Guyke187's avatar

Laravel 7 Eloquent models, get records based on user_id in parent table (to check if it matches with Auth::id)

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'businessDeveloper.user_id' in 'where clause' (SQL: select * from opportunities where businessDeveloper.user_id = 3)

Hello, I'm using Laravel 7 and eloquent models for a school project. We were told not to use joins and besides making a custom JSON string with the formatted data which will take a lot of work since I have the same problem for other functionalities as well. I got no clue how to solve my problem. The tables are like this: Opportunities(business_developer_id FK) belongTo-> businessDevelopers(user_id FK) belongTo-> user So I try to show all opportunities for a logged-in business developer. But this doesn't seem to work:

public function qryOpportunities()
    {
        $opportunities = Opportunity::where('businessDeveloper.user_id', Auth::id())->with('businessDeveloper.user', 'consultantOpportunity.consultant.user', 'contactPerson', 'customer', 'headOfDepartment.user')->get();
        return $opportunities;
    }

A solution to show all opportunities from the logged-in business developer is much appreciated. Thanks in advance.

0 likes
6 replies
Snapey's avatar

what are your tables called?

What are your models called?

What relationships have you created in your models?

Guyke187's avatar

I'll keep it to the relevant tables, models, and relationships otherwise it might be much. Since it's just the where that I can't seem to do on the parent table:

Tables:

Schema::create('opportunities', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_developer_id');
			...
			$table->foreign('business_developer_id')->references('id')->on('business_developers')->onDelete('cascade')->onUpdate('cascade');
Schema::create('business_developers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
			...
			$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
Schema::create('users', function (Blueprint $table) {
            $table->id();
			...

Models:

class Opportunity extends Model
{
    public function businessDeveloper()
    {
        return $this->belongsTo('App\BusinessDeveloper')->withDefault();
    }
}
class BusinessDeveloper extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User')->withDefault();
    }

    public function opportunities()
    {
        return $this->hasMany('App\Opportunity');
    }
}
class User extends Authenticatable
{
public function businessDevelopers()
    {
        return $this->hasMany('App\BusinessDeveloper');
    }
}

Will this do?

Snapey's avatar

In your with statement, you mention the relationship names not the table and column names,

user has many business developers and business developer has many oportunities

So, what (in words) are you trying to get?

Guyke187's avatar

I got someone to help me with a working solution. I was trying to get "all opportunities from the logged-in business developer" On the opportunity there is a FK to the business developer that the opportunity belongs to. Business developer has a FK to the users table. user table is even irrelevant since I check Auth::id with user_id from the business developers table. My apologies for the bad explaination. I think this solution will make it clear what i was trying to do:

public function qryOpportunities()
{
    $opportunities = Opportunity::whereHas('businessDeveloper', function($q) {
        $q->where('user_id', Auth::id());
    })
    ->with('businessDeveloper.user', 'consultantOpportunity.consultant.user', 'contactPerson', 'customer', 'headOfDepartment.user')
    ->get();
    return $opportunities;
}
Snapey's avatar

but WHY? does user have MANY business developers

public function businessDevelopers()
    {
        return $this->hasMany('App\BusinessDeveloper');
    }
1 like
Guyke187's avatar

You're totally right. I never gave it a second thought since I didn't design the database. It's a school project. I can't come up with a reason. Should be one on one relationship. Thanks for pointing that out.

Please or to participate in this conversation.