我有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