I am trying to model with Laravel a database for purchases, in my mysql the relations are [ (for each table in the mysql i have one model)
so I was modeling in Laravel, for the associative table "fornecedor_item_detalhe" I used relation "belongstoMany", it worked out using tinker.
Now comes the problem:
together with the detalhes("fornecedor_detalhe") and the "items" I would like to be able to access the other tables related to "fornecedor_detalhe" which are "fornecedor" and "formpagto"."Has Many Through" will work For this?
And also I would like to link this associative table "fornecedor_detalhes-Item" in relation n: m with another table to create another associative table "fornecedor_detalhe_item_rci".
I've created a model for the pivot table "fornecedor_detalhe_item", but I'm not exactly sure how to do that, as long pivot tables don't have a primary key, how can i reference to them in the other pivot table
I did not codify anything, I'm just trying to model the same thing in laravel.
If you have any suggestions about MER I also accept it.
Thank you
just use Eloquent: Relationships
look in this link realtion types
and you should have model for fornecedor and formpagto
by build relation in model hasMany or belongsTo as your database structure you can call the anywhere just by type relation name
for example
in User Model
public function post()
{
return $this->hasMany(Post::class);
}
when you call in blade $user->post you get all posts related to this user and so on
Related
I'm using Laravel and Mysql.
I'm trying to come up with a name for my pivot table where one column is a foreign key to the products table and the other column is a foreign key to the users table. I want the table name to be product_point_of_contact. I don't want to call the table product_user as that doesn't really explain what the table does. My columns would be product_id and user_id. I don't want to name the user_id column point_of_contact_id because I don't have a point_of_contacts table.
What is the convention and best option for this? Would you also create a PointOfContact model and extend it to use the User model?
The Laravel convention is:
To determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:
return $this->belongsToMany(Role::class, 'role_user');
In addition to customizing the name of the intermediate table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
So, if your models are Product and User, and you name the intermediate table as product_point_of_contact, and the fk product_id and user_id.
In your User model use:
return $this->belongsToMany(Product::class, 'product_point_of_contact');
In your Product model use:
return $this->belongsToMany(User::class, 'product_point_of_contact');
And you will not need to specify the foreign keys, since these do comply with the convention.
Said that...
What is the convention and best option for this?
IMO, the one you (and your team) can understand, and makes sense in the business logic of your application. Even if you/they read the code or the name of the tables within 6 months.
Would you also create a PointOfContact model and extend it to use the User model?
Not needed, you can use the User model. What I would do here would be to name the relationship with the meaning I want to give. For example:
class Product extends Model
{
/**
* The product point of contact (which is an user).
*/
public function pointOfContact()
{
return $this->belongsToMany(User::class, 'product_point_of_contact');
}
}
product_user will be the best since laravel itself will create the table name in alphabetical order, i advice you use product_user.
Since I designed my db, I came out with a self related model in my entity relational diagram. I have the entity country and the relation that go from country to the same country that is called passyingby and it has its own attributes transfer description.
So I would have tables:
country(nameCountry), passyingby(nameCountry1,nameCountry2,transfer_description).
How can I manage this in Laravel considering also the unique values of passyingby rows? I have created a migration to create the table country and its related model.
I am going to create a website which has exactly same as IMDB.com site logic.
I have lists table that can have lots of movies from movies table. now I want to know which method is the best choice?
Creating a ManyToMany relation between lists and movies table and create a pivot table to store ids from both tables.
In lists table create a field named movieId and store all movies ids like an array in this field and separate them with ','.
Please let me know what way would you choose and why?
Ok. Go for ManyToMany Relationship. Eloquent already has the mechanism to handle this relationship reducing your effort of writing code. Defining the proper relation via pivot table will make you much more easier for handling CRUD operation too.
With the second approach you won't be able to handle this using Eloquent.And again this is not the standard way of creating relationship between tables. Besides you have to manually prepare the name of the movies separated by comma.
I'm a new to Grails and have started a project, but I'm having troubles finding out what is wrong:
The project is already connected to my database (SQL) and it has a few many-to-many to relationship with more than 1 "parameter", like this:
static hasMany = [rules:AvaliateRules,professors:Professor,candidates:Candidate];
I run the application with no problems, but when I used show tables the transaction tables weren't all created. It just created the last parameter's table (candidate).
Any idea about the reason and how to fix it? everywhere I checked, people did the same as me and had no problems with the tables. I'm using grails 2.4.4 version.
I don't think that your current domain has many-to-many relationship with all entities that you mentioned. It might be a one-to-many relationship. Let's say your current domain is "DomainA". If only other domain like AvaliateRules,Professor has "DomainA" in hasMany then it makes it a many-to-many association.
In one-to-many association, only a column is added at the child level(many side) which contains a parentId to denote who the parent is.
I have a relation represented in MySQL that is
Classified belongsTo Category
Category hasMany Classifieds
Previously, I have had Classified.category_id set, but some Classifieds do not have Categories, and many Categories do not have any ads. This relationship is optional.
I have normalized the database to represent the database in a third table, categories_classifieds.
categories_classifieds
----
classified_id (primary, not null)
category_id (not null)
The reason this was normalized this way is to avoid null values, and also to
I am trying to create Models for the database in CakePHP 2, and there does not seem to be support for this simple relationship. Is this possible? Does the categories_classifieds join table need to be made into its own Model?
Thanks in advance for any insight.
Here are crow's-feet notation ER diagrams of the current, and proposed schemas:
http://i.stack.imgur.com/1BBVV.jpg
This is a hasAndBelongsToMany relation.
Make sure to follow cake's conventions though and make a seperate id field as primary key for the connecting model. You may not even need to have a model created for it and let it all to automagic after building the necessary relations in the models.