I have a table 'shop_visit_count' with columns ( 'user_id', 'shop_id', 'visit_count', 'created_at', 'updated_at' ),
A user can have many restaurant visit_counts, A restaurant can have many different user visit_counts of the same restaurant
the visit_count will be updated everytime the user visits a restaurant
Should I convert this to a pivot table?, or Should I add/keep the incremental Id primary key field?
If I need to convert to a pivot table, what should I name this many-to-many relationship? since this is not like the usual 'user', 'shop', 'shop_user' type relationship, but about the visit_count?
You can keep the table "shop_visit_count", it is ok. Pivot Tables don't bring much more.
Have you troubles with that actual table ?
Related
I have 3 models Eg: Model-1, Model-2, Model-3.
Model-2 and Model-3 has many to many relationship,
So i'm keeping one pivot table like Model-2_Model-3.
Model-1 is has one relationship with model-2 and i'm listing all the data from model-1,
I need to search one column from pivot table for listing Model-1.
For Eg:
$data = Model-1::with('Model-2')->get();
I need to search Model-1 Using pivot table (Model-2_Model-3).
I return pivot connection code already. Pivot is working very well.
You can query pivot table with wherePivot()
Model-1::with('Model-2')->wherePivot('field_name', $value)->get();
If the pivot values are not added, you need to add it as withPivot('field_name')
You may like to use contains, as for many to many relationship you will get collection of second model something like $user->roles, so here you can check if $user->roles->contains('field_name'). as well as $user->roles()->wherePivot('role_id', 1)->get()->isEmpty() or $user->roles()->wherePivot('role_id', 1)->get() depending on your requirement.
I have a table where I need to save the ids in array, you can see the items in
events_who is a foreign keys. Any solution where I can create a Relationship in my Model to get the data from the foreign keys? I tried belongsToMany and it doesn't work. :(
Any suggestions?
Let's assume we are having 2 models: Event and Person
And a Person can participate to multiple Event
Based on the mentioned relationships, you need to create a pivot table called event_person and define two belongsToMany() relationships in both models:
In the Person model, the relationship will look like:
public function events()
{
return $this->belongsToMany(Event::class, 'event_person');
}
Laravel / MySQL (Relational Database) don't really work this way. You should check out Many to Many Relationships in this case.
Example table schema/layout:
users
|id|name|password|
events
|id|title|body|
event_user (pivot table)
|event_id|user_id|
Usually there won't be an array in a column, you should use tables instead. Besides, normally a foreign key would be a table name(singular) following by _id.
Lets say I have a user and appointment table.
users - id, name, email, type(doctor/patient)
appointment- id, doctor_user_id, patient_user_id
My user table data-
And my appointment table data-
I could make two belongs to relationships from appointment with user table.
As you can see my appointment table, I want to store only user that type is doctor to doctor_user_id and patient to patient_user_id. But in this case i can add any user id to doctor_user_id field either it is doctor or patient but i want to add only user id as doctor_user_id only if its type is doctor.
I know how to achieve this with two different table but I was wondering is there any way to achieve this with single user table, Thanks.
Yes, you can achieve this by creating one user table only.
Create 2 foreign key for the "doctor_user_id" and "patient_user_id" of appointment table which references to User table.
You can use two belongsToMany() relationships and use the table as I pivot:
public function doctors()
{
return $this->belongsToMany(User::class, 'appointment', 'patient_user_id', 'doctor_user_id');
}
public function patients()
{
return $this->belongsToMany(User::class, 'appointment', 'doctor_user_id', 'patient_user_id');
}
If you'll use the appointment table a lot, you can also add two hasMany() relationships to the Appointment model and two belongsTo() relationships to the User model. So, you could use belongsToMany(), hasMany() and belongsTo() relationships simultaneously for in this case.
I am using Laravel 5.2.
I have 3 tables: book, user and book_user (the pivot table).
I want to use soft deletes on my pivot table. When I attach a book to a user, the relationship is inserted into the book_user table. However when I detach this relationship, the record in the pivot table is deleted even though I added use SoftDeletes to the pivot table model.
How can I implement soft deletes for the records in my pivot table when I attach or detach?
I didn't try it using soft delete with pivot table but you're saying that it doesn't work.
Just an idea, maybe you can use sync instead of detach like this;
Before that you need to add deleted_at column to book_user table as DATETIME instead of TIMESTAMP. Because new version of MYSQL doesn't support NULL for TIMESTAMP type.
Soft Deleting
$user->books()->sync(array(1 => array('deleted_at' => DB::raw('NOW()'))));
Getting
Also you can put a constraint on the Eager Load:
public function books()
{
return $this
->hasMany('Book')
->whereNull('book_user.deleted_at');
}
I have 3 tables,
users(id) , userproduct (user_id,product_id) and product (id).
When a user adds a product their user_id and the product_id is saved in USERPRODUCT table.
Now at the same time another user can save a product (kind of like "set as favorite") for this I need to save the user_id of the user who saved it and the product_id of the product. This will create another table (call it favorites) however this table is also a resolve to the many to many relationship between "users" table and "product" table.
the question: is it possible for this, to have 2 tables resolving a many to many relationship or is there any other way to solve my issue.
if it is possible how will one be draw this in the ER diagram
it depends on what you want.
you may use only one table and design your relations with boolean fields
USERS(id)
PRODUCTS(id)
USERPROD(userid, productid, isbought, isfavorite, isowned)
or you may use one many-to-many support table for every relation is up to you
maybe you want another field "owner" in PRODUCTS table to store the user id. this is a one-to-one relation
What you want to do is actually describe the kind of relation between user and product: a favorite, or he 'owns' it.
Just add another field to the userproduct-table defining the relation: favorite or owned