I have three tables:
- product_lines (id, name)
- project_posts (id, project_id, title)
- project_products (id, project_post_id, product_line_id)
So it's a many to many relationship using project_products as the pivot table. This is how I have my relationship set up in the Post model:
public function products()
{
return $this->belongsToMany('\Duplicolor\Models\Product\Line', 'project_products', 'project_post_id', 'product_line_id');
}
Then I run this query on a Post object:
public function linked_products()
{
return $this->products()
->get(array('name'));
}
I get this data back:
array (size=1)
0 =>
array (size=2)
'name' => string 'Headlight Restoration Kit' (length=25)
'pivot' =>
array (size=2)
'project_post_id' => int 57
'product_line_id' => int 5
How do I also select the ID in the pivot table? And what if they have the same column names for another column I want to select? Do I just have to write the table name down like : product_lines.id ? If not, it throws an ambiguous error. I didn't know if there was a better way of doing so.
Thanks.