@Snapey Ok, I'll add it now
These are my migrations
// Test 1 table
public function up(): void
{
Schema::create('test1s', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('desc')->nullable();
$table->timestamps();
});
}
next...
// Test 2 table
public function up(): void
{
Schema::create('test2s', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('desc')->nullable();
$table->foreignId('test2_id')->references('id')->on('test1s')->onDelete('cascade');
$table->timestamps();
});
}
next...
// Test 3 table
public function up(): void
{
Schema::create('test3s', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('desc')->nullable();
$table->foreignId('test3_id')->references('id')->on('test2s')->onDelete('cascade');
$table->timestamps();
});
}
Here are my models and methods in them
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Test1 extends Model
{
use HasFactory;
public function method1(){
return $this->hasMany(Test2::class, 'test2_id', 'id')->with('method2');
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Test2 extends Model
{
use HasFactory;
public function method2(){
return $this->hasMany(Test3::class, 'test3_id', 'id');
}
}
Here's controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Test1;
class HomeController extends Controller
{
public function index()
{
$res= Test1::with('method1')->get();
return view('home' , compact(['res']));
}
}
The tables are filled with test data, no more than 10 records... At the same time, through the debugger it shows 5 queries and 20 models... If you add records, the number of queries remains 5, and the number of models increases... Moreover, if there are about 1000 records, then There will be more than 10,000 models... Is it possible to optimize this somehow? How?