I'm struggling to get eager loading working across 3 relationships.
When I try the code below I get the data I need but it makes a ton of queries as per debugbar
public function getIndexPage(){
$brands = Brand::with(['brandAccounts','brandAccountLogs'])->orderBy('total_audience', 'DESC')->get();
return view('social-media-tracker.index')->with('brands', $brands);
}
Most examples seem to do it like this:
public function getIndexPage(){
$brands = Brand::with(['brandAccounts.brandAccountLogs'])->orderBy('total_audience', 'DESC')->get();
return view('social-media-tracker.index')->with('brands', $brands);
}
But I dont get the data from the 3rd relationship like this.
The database tables look like the following:
Stores the base brand infomation
Schema::create('brands', function(Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('slug')->nullable();
$table->string('url')->unique();
$table->integer('total_audience')->unsigned();
$table->timestamps();
$table->softDeletes();
});
Stores information about specific instances of accounts on social networks
Schema::create('brandAccounts', function(Blueprint $table) {
$table->increments('id');
$table->integer('brand_id')->unsigned();
$table->foreign('brand_id')->references('id')->on('brands');
$table->enum('network', array('facebook', 'twitter', 'instagram','youtube'));
$table->string('network_id');
$table->timestamps();
$table->softDeletes();
});
Stores individual data points for specific social media accounts.
Schema::create('brandAccountLogs', function(Blueprint $table) {
$table->increments('id');
$table->integer('brand_account_id')->unsigned();
$table->foreign('brand_account_id')->references('id')->on('brandAccounts');
$table->integer('log')->unsigned();
$table->timestamps();
$table->softDeletes();
});