アイテムモデルとレコードモデルの関係は次のように定義する必要があります。
アイテムモデル:
public function records()
{
return $this->hasMany(Record::class,'item_id');
}
レコードモデル:
public function item()
{
return $this->belongsTo(Item::class,'item_id');
}
クエリでは、次のように「doesntHave」を使用します。
$itemsWithoutRecords= Item::doesntHave('records')->get();
'count'の方法が好きな場合は、次のように 'withCount'を使用できます。
$itemsWithoutRecords= Item::withCount('records')->having('records_count','<',1)-> get();
注意:どちらの方法でも、モデル間の正しい関係が必要です
最初の方法: https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-absence
2番目の方法: https://laravel.com/docs/7.x/eloquent-relationships#counting-related-models