Johannes93's avatar

Eloquent 'with()' has no effect

EDIT: link to git here git.diebold-it.de/johannes/Eloquent_with_Problem.git

ORIGINAL: Hi all, it's my first post, please be kind :) I don't know how to make the code view here... I am using Laravel with Inertia and Vuejs and integrated Jetstream yesterday. Before, I was using Breeze without Inertia and my Code was working fine. Today I figured, when running building a query like:

'$questions= Question::where('id', 1)->with('protectionclass')->toSql();' 

the output is: "select * from questions" Eloquent simply neglects the 'with'. I tried this on multiple different relationships that were all working before. My relationship in the Question-Model:

    public function protectionclass(){
        return $this->belongsTo(ProtectionClass::class, 'protectionclass_id');
    }

The relationship in ProtectionClass-Model:

    public function questions(){
        return $this->hasMany(Question::class);
    }

The fields are named correctly, but for sake of completeness the corresponding migration-lines of questions-Schema:

            $table->unsignedBigInteger('protectionclass_id');
            $table->foreign('protectionclass_id')->references('id')->on('protectionclasses');

the table 'protectionclasses' exists as such Both tables are filled with data. I am using Laravel v 10.1.5

Does anybody have a hint for me? It has been bothering me for hours now... Thank you very much!

BR Johannes

0 likes
9 replies
webrobert's avatar

please format your code with backticks


```
  // code
```

tykus's avatar
tykus
Best Answer
Level 104

Whenever you eager-load relations using with, there are two queries; one for the Model and one for its relation(s) - whenever you execute with toSql, you will see only the Model query, because you never get the ID(s) for the second query.

You can use the following if you want to see the queries;

\DB::listen(fn($query) => dump([$query->sql, $query->bindings]));
$questions= Question::where('id', 1)->with('protectionclass')->first();' 

This will also execute the queries on the database.

1 like
Johannes93's avatar

thank you for the quick reply. did that, the output is:

array:2 [▼ // app/Http/Controllers/QuestionController.php:18
  0 => "select * from `protectionclasses` where `protectionclasses`.`id` in (1)"
  1 => []
]

I ran that query in heidisql on just the same database and it returns the correct row. but still, the 'with' is not working in Eloquent. Joins work... but that wouldn't solve the problem

tykus's avatar

That query does not look right; you have no limit which would be expected of a belongsTo relation.

Johannes93's avatar

I only pasted the part of protectionclasses before. the whole thing is:

array:2 [▼ // app/Http/Controllers/QuestionController.php:18
  0 => "select * from `questions` where `id` = ? limit 1"
  1 => array:1 [▶]
]

array:2 [▼ // app/Http/Controllers/QuestionController.php:18
  0 => "select * from `protectionclasses` where `protectionclasses`.`id` in (1)"
  1 => []
]
tykus's avatar

Do you actually have a protectionclasses record where the ID is 1; what happens if you just query for that record?

Protectionclass::find(1)
Johannes93's avatar

Yes, I do,

        $protectionClass = ProtectionClass::find(1);

it returns

  #attributes: array:6 [▼
    "id" => 1
    "name" => "Voraussetzung"
    "min_threshold" => 0.95
    "max_threshold" => 1.0
    "created_at" => "2023-02-26 16:10:10"
    "updated_at" => "2023-02-26 16:10:10"
  ]
Johannes93's avatar

I reduced the problem and pushed it in my own git: git.diebold-it.de/johannes/Eloquent_with_Problem.git

Would you mind having a look at it? Thank you very much.

Please or to participate in this conversation.