mysql Laravel Eloquent toSql()缺少连接表

des4xlb0  于 4个月前  发布在  Mysql
关注(0)|答案(1)|浏览(68)

我有一个关系,当我使用with("relationship")时,它实际上正确地返回了连接值。问题是我怀疑它使用了一个循环来获取值,所以我想检查生成的SQL。问题是生成的SQL根本不包括连接表。

json_encode(User::find(1)->materials()->where("id", 1)->with("exerciseRecords")->get()->toArray());

字符串
正确给出:

"[{"id":8091,...,"exercise_records":[{"id":28,...,"updated_at":"2023-12-09T01:22:51.000000Z"}]}]"


> User::find(1)->materials()->where("id", 1)->with("exerciseRecords")->toSql();


只是给

"select * from `materials` where `materials`.`user_id` = ? and `materials`.`user_id` is not null and `id` = ?"


这是否意味着我应该解释为肯定 does 使用循环?或者toSql不能被信任?

yquaqz18

yquaqz181#

要确认Laravel是否使用单独的查询,您可以使用数据库查询log,这将返回所有执行的查询的数组,包括用于急切加载关系的查询。您将看到在初始查询获取材料之后,还有另一个查询获取exerciseRecords用于加载材料。

DB::enableQueryLog();
User::find(1)->materials()->where("id", 1)->with("exerciseRecords")->get();
$queries = DB::getQueryLog();
return $queries;

字符串

相关问题