Laravel relationships in CRUD application
I want to create a CRUD application with Laravel with the following scenario:
we have a mine in which various trucks take mining material from a mining hole and though a road they deposit it in a dump hole. Now each road connects an area with a dump hole. So "Road No1" takes material and from "Area 1" and disposes into "dump hole 1". Road No2 takes material from "Area 1" as well and might dispose it to "dump hole 1" too. So areas and roads have one-to-many relationship like one area can have many roads and dump holes with roads should have a one-to-many relationship as well, like one dump hole gets material from many roads.
Taking for granted that areas, roads and dump holes are three different tables, is it completely necessary to create a relationship between these three tables as mentioned above ? And if yes I was wondering if this will create duplicate values in the roads table in case the user want to see the dumps site that a road put material to or an area the roads come from. For example consider the following table:
+----+-------------+----------+-------+---------+---------------+
| id | route | distance | slope | area | dumpsite |
+----+-------------+----------+-------+---------+---------------+
| 2 | First route | 10000 | 20 | E12 | Big Dumpsite |
| 3 | Second route| 2000 | 15 | E12 | Huge Dumpsite |
| 4 | Third route | 2555 | 10 | E13 | Big Dumpsite |
+----+-------------+----------+-------+---------+---------------+
as you can see in area and dumpsite columns there duplicate values.
Is this good design ?
mysql laravel
add a comment |
I want to create a CRUD application with Laravel with the following scenario:
we have a mine in which various trucks take mining material from a mining hole and though a road they deposit it in a dump hole. Now each road connects an area with a dump hole. So "Road No1" takes material and from "Area 1" and disposes into "dump hole 1". Road No2 takes material from "Area 1" as well and might dispose it to "dump hole 1" too. So areas and roads have one-to-many relationship like one area can have many roads and dump holes with roads should have a one-to-many relationship as well, like one dump hole gets material from many roads.
Taking for granted that areas, roads and dump holes are three different tables, is it completely necessary to create a relationship between these three tables as mentioned above ? And if yes I was wondering if this will create duplicate values in the roads table in case the user want to see the dumps site that a road put material to or an area the roads come from. For example consider the following table:
+----+-------------+----------+-------+---------+---------------+
| id | route | distance | slope | area | dumpsite |
+----+-------------+----------+-------+---------+---------------+
| 2 | First route | 10000 | 20 | E12 | Big Dumpsite |
| 3 | Second route| 2000 | 15 | E12 | Huge Dumpsite |
| 4 | Third route | 2555 | 10 | E13 | Big Dumpsite |
+----+-------------+----------+-------+---------+---------------+
as you can see in area and dumpsite columns there duplicate values.
Is this good design ?
mysql laravel
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35
add a comment |
I want to create a CRUD application with Laravel with the following scenario:
we have a mine in which various trucks take mining material from a mining hole and though a road they deposit it in a dump hole. Now each road connects an area with a dump hole. So "Road No1" takes material and from "Area 1" and disposes into "dump hole 1". Road No2 takes material from "Area 1" as well and might dispose it to "dump hole 1" too. So areas and roads have one-to-many relationship like one area can have many roads and dump holes with roads should have a one-to-many relationship as well, like one dump hole gets material from many roads.
Taking for granted that areas, roads and dump holes are three different tables, is it completely necessary to create a relationship between these three tables as mentioned above ? And if yes I was wondering if this will create duplicate values in the roads table in case the user want to see the dumps site that a road put material to or an area the roads come from. For example consider the following table:
+----+-------------+----------+-------+---------+---------------+
| id | route | distance | slope | area | dumpsite |
+----+-------------+----------+-------+---------+---------------+
| 2 | First route | 10000 | 20 | E12 | Big Dumpsite |
| 3 | Second route| 2000 | 15 | E12 | Huge Dumpsite |
| 4 | Third route | 2555 | 10 | E13 | Big Dumpsite |
+----+-------------+----------+-------+---------+---------------+
as you can see in area and dumpsite columns there duplicate values.
Is this good design ?
mysql laravel
I want to create a CRUD application with Laravel with the following scenario:
we have a mine in which various trucks take mining material from a mining hole and though a road they deposit it in a dump hole. Now each road connects an area with a dump hole. So "Road No1" takes material and from "Area 1" and disposes into "dump hole 1". Road No2 takes material from "Area 1" as well and might dispose it to "dump hole 1" too. So areas and roads have one-to-many relationship like one area can have many roads and dump holes with roads should have a one-to-many relationship as well, like one dump hole gets material from many roads.
Taking for granted that areas, roads and dump holes are three different tables, is it completely necessary to create a relationship between these three tables as mentioned above ? And if yes I was wondering if this will create duplicate values in the roads table in case the user want to see the dumps site that a road put material to or an area the roads come from. For example consider the following table:
+----+-------------+----------+-------+---------+---------------+
| id | route | distance | slope | area | dumpsite |
+----+-------------+----------+-------+---------+---------------+
| 2 | First route | 10000 | 20 | E12 | Big Dumpsite |
| 3 | Second route| 2000 | 15 | E12 | Huge Dumpsite |
| 4 | Third route | 2555 | 10 | E13 | Big Dumpsite |
+----+-------------+----------+-------+---------+---------------+
as you can see in area and dumpsite columns there duplicate values.
Is this good design ?
mysql laravel
mysql laravel
asked Nov 20 at 22:12
Stefanos
54
54
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35
add a comment |
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35
add a comment |
1 Answer
1
active
oldest
votes
From what I can understand, you would setup your models like this
class Area extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access areas possible dumpsites
public function getDumpsitesAttribute()
{
return $this->roads->with('dumpsite')->get()->pluck('dumpsite')->unique();
}
}
class Road extends Model
{
public function area() {
return $this->belongsTo('AppArea');
}
public function dumpsite() {
return $this->belongsTo('AppDumpsite');
}
}
class Dumpsite extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access dumpsites possible area
public function getAreasAttribute()
{
return $this->roads->with('area')->get()->pluck('area')->unique();
}
}
And your road
table would have area_id
and dumpsite_id
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402384%2flaravel-relationships-in-crud-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From what I can understand, you would setup your models like this
class Area extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access areas possible dumpsites
public function getDumpsitesAttribute()
{
return $this->roads->with('dumpsite')->get()->pluck('dumpsite')->unique();
}
}
class Road extends Model
{
public function area() {
return $this->belongsTo('AppArea');
}
public function dumpsite() {
return $this->belongsTo('AppDumpsite');
}
}
class Dumpsite extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access dumpsites possible area
public function getAreasAttribute()
{
return $this->roads->with('area')->get()->pluck('area')->unique();
}
}
And your road
table would have area_id
and dumpsite_id
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
add a comment |
From what I can understand, you would setup your models like this
class Area extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access areas possible dumpsites
public function getDumpsitesAttribute()
{
return $this->roads->with('dumpsite')->get()->pluck('dumpsite')->unique();
}
}
class Road extends Model
{
public function area() {
return $this->belongsTo('AppArea');
}
public function dumpsite() {
return $this->belongsTo('AppDumpsite');
}
}
class Dumpsite extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access dumpsites possible area
public function getAreasAttribute()
{
return $this->roads->with('area')->get()->pluck('area')->unique();
}
}
And your road
table would have area_id
and dumpsite_id
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
add a comment |
From what I can understand, you would setup your models like this
class Area extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access areas possible dumpsites
public function getDumpsitesAttribute()
{
return $this->roads->with('dumpsite')->get()->pluck('dumpsite')->unique();
}
}
class Road extends Model
{
public function area() {
return $this->belongsTo('AppArea');
}
public function dumpsite() {
return $this->belongsTo('AppDumpsite');
}
}
class Dumpsite extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access dumpsites possible area
public function getAreasAttribute()
{
return $this->roads->with('area')->get()->pluck('area')->unique();
}
}
And your road
table would have area_id
and dumpsite_id
From what I can understand, you would setup your models like this
class Area extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access areas possible dumpsites
public function getDumpsitesAttribute()
{
return $this->roads->with('dumpsite')->get()->pluck('dumpsite')->unique();
}
}
class Road extends Model
{
public function area() {
return $this->belongsTo('AppArea');
}
public function dumpsite() {
return $this->belongsTo('AppDumpsite');
}
}
class Dumpsite extends Model
{
public function roads() {
return $this->hasMany('AppRoad');
}
// to access dumpsites possible area
public function getAreasAttribute()
{
return $this->roads->with('area')->get()->pluck('area')->unique();
}
}
And your road
table would have area_id
and dumpsite_id
edited Nov 21 at 1:22
answered Nov 21 at 1:14
CUGreen
1,9051511
1,9051511
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
add a comment |
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
ah thank you! This probably will do! A couple more questions: is it really necessary to relate tables? And also do I need to add constraints like onUpdate() and onDelete() ? What about the duplicate values that will appear on the table?
– Stefanos
Nov 21 at 23:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402384%2flaravel-relationships-in-crud-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The example you posted is the road table, right? Anyway if I understood correctly your needs, I would have a table for areas, one for dumpholes and a road pivot table that takes in the PK of areas and dumpholes plus whichever other attributes specific to the road.
– mur762
Nov 20 at 23:35