Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 [Help] Laravel eloquent, working with tables

views
     
JusticeDeserves
post Jun 19 2017, 07:05 PM

Regular
******
Senior Member
1,914 posts

Joined: Aug 2009


Looks like you might need to use hasManyThrough?
https://laravel.com/docs/5.4/eloquent-relat...as-many-through
JusticeDeserves
post Jun 20 2017, 04:57 PM

Regular
******
Senior Member
1,914 posts

Joined: Aug 2009


Model in Table A:
CODE

public function tablec()
{
return $this->hasManyThrough('App\TableC', 'App\TableB', 'tableB_id', 'id');
}


Controller:
CODE
$enteries = TableA::get();
foreach($enteries as $ent) {
var_dump($ent->tablec);
}


Not entirely sure this code is correct or not, you'll have to play around with it.. your table structure is very similar to the one on Laravel docs so it shouldn't be too tough to get the results.

The way you're doing now is fine for relatively small tables but if your tables gets larger, it might just be better to figure it out using Eloquent because you can lazy load the stuff which means less heavy queries.

This post has been edited by JusticeDeserves: Jun 20 2017, 04:57 PM

 

Change to:
| Lo-Fi Version
0.0126sec    0.28    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 08:16 PM