我有Question
和Answer
模型。 问题有很多答案。在php artisan tinker
模式下运行下面的命令调用数据库查询无明显原因:如何在不执行数据库查询的情况下创建新记录?
$q = new Question;
$q->answers[] = new Answer; // invokes the below query
// the executed query
select * from `answers` where `answers`.`question_id` is null and `answers`.`question_id` is not null
正如你看到的,没有必要对数据库调用任何。我怎样才能防止它?
你可以查看是否'$ Q->答案() - >保存($答案)鉴于$答案已创建'运行SELECT查询? – ThePurpleK
@ThePurpleK对不起,但我不明白你的意思。查询运行是因为'$ q-> answers'的用法,正如Joel的回答指出的那样, –
我假设你的意图是将一个新的Answers记录与一个Question记录关联起来。我想知道我给出的代码是否会阻止不需要的数据库调用。 – ThePurpleK