PHP Developers Network
http://forums.devnetwork.net/

should the id always be the foreign key ?
http://forums.devnetwork.net/viewtopic.php?f=72&t=143536
Page 1 of 1

Author:  gautamz07 [ Sun Apr 23, 2017 2:47 pm ]
Post subject:  should the id always be the foreign key ?

I have a table Admin(should have actully named it articles :P ) with the following fields:

Syntax: [ Download ] [ Hide ]
ID
title
keywords
blog_content
tag
filePath
slug
created_at
updated_at


And Then i have the Tags table with the following feilds:

Syntax: [ Download ] [ Hide ]
Id
tag


Now i want to create a hasMany relationship between the Admin and the Tags table. How do i do this ? I mean which field in the Admin table will be the foreign key ? i believe usually the foreign key is always the ID field , even the docs show the same: HERE --> , or should can i make the tag feild in the Admin table as the foreign key to the ID in the Tags table ?

I still have a scrambled brain about this issue and hence i taught it would be best to ask here, can somebody please give me a bit more clarity on this topic ?

Thank you.
Gautam.

Author:  Celauran [ Sun Apr 23, 2017 4:29 pm ]
Post subject:  Re: should the id always be the foreign key ?

Why do you have a tag field in the admin table? (and yes, you should change that table name)

If you're setting up a has and belongs to many (ie. an article can have many tags, a tag can belong to many articles), then you'll want a pivot table for both IDs. See https://laravel.com/docs/5.4/eloquent-r ... ny-to-many

Author:  gautamz07 [ Tue Apr 25, 2017 4:42 pm ]
Post subject:  Re: should the id always be the foreign key ?

OK Thanks celaurian ! :)

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/