Senin önerdiğinden farklı bir şey yapmadım ki. Sanırım yanlış yazdın.
Tam emin olmak için testi ayrıntılı bir halâ getirdim.
Şuan ki haliyle kesinlikle Caching yapılıyor.
public function testCachingModelsWithITSRelation() {
// creating products..
for($i=0; $i < 100; $i++) {
Product::create(['code' => str_random(16)]);
}
$this->assertCount(100, Product::all()); // passed !
$records = [];
for($i=0; $i < 100; $i++){
$records[] = ['path' => str_random(16)];
}
// creating 100 images for each a product
for($i=1; $i < 101; $i++) {
$product = Product::find($i);
$images = array_map(function($image) use ($product) {
$image['product_id'] = (integer) $product->id;
return $image;
}, array_values($records));
//var_dump($images);
// adds 100 Images to each Product
$product->ProductImages()->createMany( $images);
}
// checking created images at just time
$this->assertCount(10000, ProductImage::all()); // passed
// All products models and them images model is cached
$cached = Product::with('ProductImages')->remember(10)->get();
// all of query log is deleting...
\DB::flushQueryLog();
$this->assertCount(0, \DB::getQueryLog()); // passed
$this->assertCount(100, $cached);
$this->assertCount(0, \DB::getQueryLog()); //passed !
$this->assertCount(100,$cached->find(1)->ProductImages->all());
$this->assertCount(0, \DB::getQueryLog()); //passed !
/**
* @evrend'nin testi
*/
$newResult = Product::with('ProductImages')->remember(10)->get();
$this->assertTrue(count(\DB::getQueryLog()) > 0 );
// all of query log is deleting...
\DB::flushQueryLog(); // tüm db query logları silindi.
$this->assertCount(100, $newResult->find(1)->ProductImages->all());
$this->assertCount(0, \DB::getQueryLog()); //passed !
// sayalım
$no = 0;
foreach ($newResult->random()->ProductImages->all() as $image) {
$image->path;
$image->id;
$image->product_id;
$no++;
}
// Görültüğü gibi "0" sorgu
$this->assertCount(0, \DB::getQueryLog()); //passed !
$this->assertEquals(100, $no); // 100 adet Image return edilmiş.
}