Regarding Relationship Of DB Tables In Laravel
Suppose we have many to one relationship between table A and table B. Now there are two possibilities to define this relation in Laravel.
while creating migration I use the "References" Keyword. and the run the migration
The other way is I use hasMany and belongsTo in model class corresponding to those tables and do not mention References and all in migration tables.
Can some one help me in understanding what approach I should follow. Or we need to have both the approach in our codebase to create relationships. Can some one throw some light on it. I am kind of confused. I am new to Laravel and learning these intricacies. thank you.
Answer
Actually, you should use both 1 and 2 to make Eloquent relations work. If you're using raw queries, you can use only 1, but the real power of Laravel is in Eloquent model relations.
Eloquent assumes the foreign key of the relationship based on the model name
https://laravel.com/docs/5.1/eloquent-relationships#defining-relationships
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms