I am working on a project with has this kind of an model.
A vendor's table to store all vendor details - A vendor is like a company that offers some service.
vendor
id | name
----+-------
1 | abc
2 | def
Then I have a Services table which contains the list of all the services that a vendor can offer:
service
id | name
---+----------------
1 | abc
2 | def
3 | ghi
And there is an area table which contains the list of all the areas from which vendor's can chose if they want to provide service in that area or not.
Areas
id | name
---+------
1 | abc
2 | def
3 | ghi
Now I want to have a pivot table that stores the details of which vendor provides which service in which area and at what price so my pivot table structure is like this:
vendor_id | service_id | area_id | price
-----------+------------+---------+---------
1 | 1 | 2 | 25.00
1 | 1 | 1 | 24.00
2 | 1 | 1 | 23.00
and so on ...
So, now I have 3 different eloquent models for areas services and vendors; however, as the pivot table contains 3 foreign keys, I am unable to update the pivot table properly each time a vendor changes his preferences.
Can you please suggest me the way to define relationship in this scenario or should I change my table structure to store this data?
The following article from 2013 did the job for me.
http://developed.be/2013/08/30/laravel-4-pivot-table-example-attach-and-detach/
The setup is something like below
Users
id | name | email
Projects
id | title | details
Roles
id | title
Project_User
id | project_id | user_id | role_id
class Project extends Eloquent
{
public function users(){
return $this->belongsToMany('User','project_user','project_id','user_id')
->withPivot('role_id')
->join('roles','role_id','=','roles.id')
->select('roles.title as pivot_roles_title');
}
}
I ran into this issue today.
You can use the syncWithPivotValues() method, passing the extra foreign key(s).
$vendor->services()->syncWithPivotValues($service_id, ['area_id' => $area->id])
https://laravel.com/docs/8.x/eloquent-relationships#syncing-associations
Related
I have two tables where some same kind of information kept. One table has approved information and other one contains pending(waiting for approval) data. I fetch data from both table and display in a same view. So user will see data from both the tables. User can delete those records. But when deleting I've a trouble with finding out which table I should delete.
Assume, table1(Approved info), table2(Pending info)
table1
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 100 |
11 | test2 | N/A | 100 |
12 | test3 | N/A | 101 |
13 | test4 | N/A | 200 |
table2
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 105 |
11 | test2 | N/A | 103 |
12 | test3 | N/A | 106 |
13 | test4 | N/A | 202 |
table1 has a record with id of 10; and table2 has a record with id of 10 in that table. Id is the primary key of both tables. Both record will show to user. Let's say user wants to delete the record related to id 12 came from table2. So I want to delete that record from table2. But how can I figure out which table to delete that record. Because I can't use id to figure out the table. I have tried using some kind of data attribute attached with
data coming from table2 to differentiate them. But anyone can change them by inspecting it. So what is the proper way for solve this issue?
On any case, on any system, makes sense to have two to tables with same columns. That should be one of the firsts rules of database design. What's more, you discovered yourself how hard is to maintain a design like that. I see this on legacy systems developed with zero love to the code. In the future this will turn into a snowball. You should change it as soon as possible.
status column
The status of and entity or resource, is classic requirement, usually implemented with one little column which called : status, flag, mode, etc. In your case, it could have these values (#BhaumikPandhi comment):
pending/approved/rejected
id | name | description | creator | status |
--------------------------------------------
10 | test1 | N/A | 100 | pending|
If you are worried to the database optimization, you could use a tinyint with these equivalence in your documentation:
1 = pending
2 = approved
3 = rejected
status table
You could keep your first table called record
id | name | description | creator |
And create another one called record_status with 2 columns, in which record_id is a FK of record table
record_id | status |
Anyway, the status column is the most easy a classic approach to your requirement.
I have two table
Treatment Table - in this table I have:
+--------------+----------------------+
| Treatment_id | Treatment_name |
+--------------+----------------------+
| 1 | Bridges |
| 2 | Root canal Treatment |
| 3 | Filling |
+--------------+----------------------+
Fee Table - in this table:
+--------+--------------+------+
| Fee_id | Treatment_id | Fee |
+--------+--------------+------+
| 1 | 1(Bridges) | 5000 |
| 2 | 2 | 6500 |
+--------+--------------+------+
Note: Here Treatment_id is Treatment_name
Here, i insert data in database then we display data in fee template in laravel
how can display Treatment_name instead of Treatment_id
pls help i am new in laravel
i want display data fee blade template
Like
Fee_id|Treatment_id|Fee
1 |Bridges|5000
instead of Treatment_id Display Treatment_name
it's work fine
but when we update Fee table then it display error
it updated but in Fee table Treatment_id column will updated Treatement_name..
suppose i update above data in Fee table Treatment_id=1=Bridge ,updated it replace Treatment_name insted of Treatment_id ,so that's why it gives error so,how update
You can do this by Laravel relationships
public function treatment()
{
return $this->belongsTo(App\Treatment::class, 'Treatment_id', 'Treatment_id');
}
App\Treatment is Model class
Treatment_id is the fields name that is used to make a relation between the two tables
After this, you can access the treatment table columns in the following way.
$fee->treatment->Treatment_name;
If you are not aware of Laravel relationships then please read about this.
Here is Laravel Relationship Doc:
https://laravel.com/docs/6.x/eloquent-relationships
I'm not sure why I'm struggling with this it seems like a very simple concept. So my struggling makes me think that perhaps my data modeling needs another component...
I'm using Laravel 5 and am trying to define some model relationships. BelongsTo,HasA, etc. Before I can write the code, I need to at least conceptually understand what type of relationship I'm creating.
I have an application to where users can send people referral links, if a person clicks on the link and signs up, their user record makes note of the code that referred them. This way I can trace back and see who referred a particular user. But a referral is NOT necessary to sign up
Tables:
USERS
+----+-------------+
| id | referral_id |
+----+-------------+
| 1 | 1 |
| 2 | null |
| 3 | 2 |
+----+-------------+
REFERRALS
+----+---------------+---------+
| id | referral_code | user_id |
+----+---------------+---------+
| 1 | 12345 | 2 |
| 2 | 54321 | 2 |
| 3 | 99999 | 2 |
+----+---------------+---------+
USERS.REFERRAL_ID references REFERRALS.ID
and
REFERRALS.USER_ID references USERS.ID
But what kind of relationships are these?
The only one that seems obvious to me is that REFERRALS.USER_ID belongs to USERS.
But what about USERS.REFERRAL_ID, saying it belongsTo Referrals doesn't feel right, as that record isn't required and I don't feel like it 'owns' the user by any means. Saying it hasA referral doesn't feel correct either, as again the user doesn't own or even require the referral.
I guess what is confusing me is that REFERRALS is an optional entity.
How should I conceptualize the relationship between USERS.REFERRAL_ID and REFERRALS.ID?
Is it bad to have this sort of "circular reference"? Would I be better off creating a pivot table?
No need to add any reference to the Referrals table in the User table, you already have that relation defined in the referral table ( user_id column )
Further reading: https://en.wikipedia.org/wiki/Database_normalization
The Relationship is
USER has many REFERRALS
REFERRAL belongs to USER ( inviter )
REFERRAL belongs to USER ( invitee )
Modify your REFERRALS table
+----+---------------+---------+------------+
| id | referral_code | user_id | invitee_id |
+----+---------------+---------+------------+
| 1 | 12345 | 2 | 1 |
| 2 | 54321 | 1 | null |
| 3 | 99999 | 3 | 1 |
+----+---------------+---------+------------+
user_id is the id of the user that sends the invitation
invitee_id is the id of the user that accepts and registers
invitee_id column is nullable() and will contain the id of the invitee from users table when they join.
Think of it as a JOIN table between inviter and invitee.
Description:
I'm working on a database setup that's requiring that I use multiple associative tables and I want to know how associative tables work within Laravel while taking advantage of the Eloquent Relationships.
I have a dataset containing soccer games. I need to store players, games, and specific player information for a game. So my structure should look something like:
Player Table:
+------------+---------+
| id | integer |
+------------+---------+
| name | string |
+------------+---------+
| team | string |
+------------+---------+
| country | string |
+------------+---------+
| average | string |
+------------+---------+
| updated_at | Date |
+------------+---------+
| created_at | Date |
+------------+---------+
Game Table
+--------------+---------+
| id | integer |
+--------------+---------+
| name | string |
+--------------+---------+
| country | string |
+--------------+---------+
| tournament | string |
+--------------+---------+
| score | string |
+--------------+---------+
| started_at | Date |
+--------------+---------+
| ended_at | Date |
+--------------+---------+
| updated_at | Date |
+--------------+---------+
| published_at | Date |
+--------------+---------+
The issue being that I need a table that associates the two together as a player can have many games and a game has many players.
Question:
How would I go about structuring my code?
Do I write a player_game model?
Do I store associations in a different way than usual because this is Laravel?
Intuitively I would want to write a player_game migration that contains game specific information for a player. But how does that work with Laravel 5 and the hasMany() attributes?
Follow your intuition. You were right, what you are looking for is called a Many to Many Relationship.
Create the intermediate table and then you can play with different queries, if Laravel HasMany type methods doesn't fill your needs, you can always query the intermediate table directly.
you need to create a pivot table named game_player many to many relationship. you can store your game specific information inside this table.
relationship in Game Model,
public funtion players()
{
return $this->hasMany('Player');
}
to access the game specific information from the pivot table,
foreach ($game->player as $player)
{
echo $player->pivot->created_at;
}
I'm trying to find out the best way store array of items with details in SQL table.
I have a user account database. The user have multiple input fields to enter multiple details like:
___ : ______ +(get more multiple field field)
User can input any details like
Output1 : Output2
Mobile : 2455...
email : sdf
city : dfs
Other : sf
On an average a user will use around 20 options
Since the fields (mobile, email etc.) are not known to me, I have to store Output1 field with the answer field (output2).
I will be having a very huge user base, so I think it's not better to create separate tables for each user.
Is there any way to store and get the details in limited or single column.
Since both the attribute name and value comes from users, a typical 3-table model of saving many-to-many relationship is a bit of overkill.
I would just kept users and their attributes in two separate tables:
+---------+-----------+--------------+
| user_id | user_name | user_email |
+---------+-----------+--------------+
| 1001 | John | john#doe.com |
+---------+-----------+--------------+
| 1002 | Tim | tim#doe.com |
+---------+-----------+--------------+
+----------+-----------+--------------+--------------+
| field_id | user_id | field_name | field_value |
+----------+-----------+--------------+--------------+
| 1 | 1001 | Option1 | Option2 |
+----------+-----------+--------------+--------------+
| 2 | 1001 | Mobile | 2345656565 |
+----------+-----------+--------------+--------------+
| 3 | 1001 | city | dfs |
+----------+-----------+--------------+--------------+
| 4 | 1002 | Other | something |
+----------+-----------+--------------+--------------+
Possibly with some additional columns for sorting, tagging, etc.
I would use 3 tables.
Table 1 - user. PK is UserId. Other fields are name, rank, serial number, etc
Table 2 - Attribute - Primary key is AttributeId. Other Field is attribute name. Examples of attribute names are emailAddress, cellphoneNumber, cityName.
Table3 - UserAttribute. Primary Key is UserId and AttributeId. Other field is Attribute value.