Welcome Guest ( Log In | Register )

Bump TopicReply to this topicRSS feed Start new topic Start Poll

Outline · [ Standard ] · Linear+

> [Help] Laravel eloquent, working with tables

Master Oogway
post Jun 19 2017, 10:38 AM, updated 4 months ago

Getting Started
**
Group: Junior Member
Posts: 54

Joined: Mar 2016


so i have 3 tables that i wanted to join using laravel eloquent

Table A
-id
-nameA

Table B
-id
-tableA_id
-nameB

Table C
-id
-tableB_id
-nameC

Question:
Is it possible that from view page of Table C can acesss the data from Table A (nameA)

so far im using DB like this
CODE
$joinedTable = DB::table('TableC')
                   ->join('TableB', 'TableC.tableB_id', '=', 'TableB.id')
                   ->join('TableA', 'TableB.tableA_id', '=', 'TableA.id')
                   ->select('TableA.*)
                   ->get();

User is offlineProfile CardPM
Go to the top of the page
+Quote Post
cubiclecarbonate
post Jun 19 2017, 02:15 PM

Casual
***
Group: Junior Member
Posts: 437

Joined: Jul 2011


it is best if you did first then check the result
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Master Oogway
post Jun 19 2017, 02:40 PM

Getting Started
**
Group: Junior Member
Posts: 54

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Jun 19 2017, 02:15 PM)
it is best if you did first then check the result
*
using DB method works fine, I just wanna know how to use eloquent
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
JusticeDeserves
post Jun 19 2017, 07:05 PM

Regular
******
Group: Senior Member
Posts: 1,907

Joined: Aug 2009


Looks like you might need to use hasManyThrough?
https://laravel.com/docs/5.4/eloquent-relat...as-many-through
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Master Oogway
post Jun 20 2017, 08:47 AM

Getting Started
**
Group: Junior Member
Posts: 54

Joined: Mar 2016


QUOTE(JusticeDeserves @ Jun 19 2017, 07:05 PM)
Looks like you might need to use hasManyThrough?
https://laravel.com/docs/5.4/eloquent-relat...as-many-through
*
how bout the structure in controller and view?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
JusticeDeserves
post Jun 20 2017, 04:57 PM

Regular
******
Group: Senior Member
Posts: 1,907

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Bump TopicReply to this topicTopic OptionsStart new topic
 

Switch to:
| Lo-Fi Version
0.0577sec    5.72    5 queries    GZIP Disabled
Time is now: 26th September 2017 - 08:40 PM