現(xiàn)象:Article::query()->with('user')->join('details','articles.id','=','details.article_id')->where('details.content','like','%test%')->get();
問題:這樣寫會導(dǎo)致model中article表的id被details表中的id覆蓋,with的時候user原本關(guān)聯(lián)的是article表的ID,此時確錯誤的關(guān)聯(lián)了details表中的ID,導(dǎo)致數(shù)據(jù)錯亂。
解決辦法:指定ID字段,或放棄join,改用子查詢
->select(['articles.id',....])
->whereIn(Detail::query()->where('content','like','%test%')->pluck('id'))