please format your code with backticks
``` // code ```
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
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.
Please or to participate in this conversation.