How to present two tables which have a relation between them like the case of the "sales" table and "sales_content" using the laravel model - eloquent

I have two tables in my database, one for "sales" informations and the other for "sale content" (items). What is the best method to present this using the laravel models? For example, is it better to create a model for each table or can we only use one?

A Model for each table - you cannot use one Model for multiple tables.
Laravel tries to infer the table name from the Model name based on lowercase pluralisation - so if your Model is called Sale it will presume that the table is called "sales". If your table is something else, you defined the table in the Model using :
protected $table = "online_sales";
Then you will need to define the relationships between the two models - the documentation is here : https://laravel.com/docs/8.x/eloquent-relationships

Related

Sequelize associate a table without defining its schema

Lets say I have a table in the DB called departments, In my code I don't need to interact directly with it and its schema is defined as a part of another service.
I want to make a new table called employees that will have a foreign key from departments.
Is there a way to make this association (referencing the table) without needing to defining the table schema again in my project?
To the best of my knowledge, there is no way to define an association between two tables one with a model defined in Sequelize and the other with no model defined in Sequelize.
To define an association between two models you must define their respective models in Sequelize.

Laravel eloquent join multiple table and search data using with() method

I have two table user and user_info. I need to join those table and have to search data from them. It is throwing error as unknown column.I have solution using DB query, Is it possible to do search using with() method in controller and eloquent relationship in model.
Thank you
It's not possible to filter models by their related models attributes using with() - this method only allows filtering related models, not the original ones you're loading.
In order to filter by attributes of related models you should use whereHas() method, e.g. in order to load all users that have country column set to uk in their user_info data you could do the following:
$usersFromUK = User::with('user_info')->whereHas('user_info', function($query) {
$query->whereCountry('uk');
})->get();

Entity Framework 6 - Inserting/Updating 2 table joined by a view

In my database I have
a Members table which contains basic member details (MemberId [primary key - auto generated number], MemberName, IsActive).
a MembersDetails table which contains more detailed information about the member (Address, Phone, Birthday ...). MembersDetails has MemberId field as a foreign key to the Members table.
There's a reason (part of the app logic) that the 2 tables are separated and are not all in one table.
I've created a view that gets a full member details (a join of the 2 tables), and Entity Framework created an object that represents the view.
I have 2 questions:
Is there a better way of flattening 2 joined tables into an object other than creating a view in the database?
I would like to create an object of the view type (a full user details), initialize it's properties and insert it to the database (which will put the info it needs in the Members table, the the generated id, and than insert to the MemberDetails table). Is there a way to do that?
Ad 1.
I think a database view will be a good choice for performance reasons.
But you can investigate an inheritance provided with the Entity Framework.
It allows you join two separated tables in one model object containing all properties (from "derived" and "base" table). Note, it will be OK for one to one relations (but not for one to many).
Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application
Ad 2.
The Entity Framework inheritance will help you with this issue well. Alternatively, if you can use a database view, just create stored procedures for inserting and updating data included in a view, then map the stored procedures for specified actions on the view model generated by Entity Framework.

Adding DateTime field to many-to-many EF Code First relationship

I am using EF 6 Code-First, table per type, and I have two concrete classes Group and User. Group has a navigation property Members which contains a collection of User. I have mapped this many-to-many relationship in EF using Fluent syntax:
modelBuilder.Entity<Group>
.HasMany<User>(g => g.Members)
.WithMany(u => u.Groups);
I would like to be able to say when a member has joined a group so that I can query for, say, the newest member(s). I am not sure of how this is best accomplished within the framework.
I see the following options:
Create and use an audit table (ie GroupMembershipAudit consisting of Group, User, join/unjoin, and DateTime
Add a column to the autogenerated many-to-many table between User and Group
Is there anything within EF to facilitate this sort of storage of many-to-many historical info like this / append columns to the many-to-many relationship?
Add a column to the autogenerated many-to-many table between User and
Group
That is not possible - auto-generated junction tables can contain only keys (that is called Pure Join Table). According to Working with Many-to-Many Data Relationships article: If the join table contains fields that are not keys, the table is not a PJT and therefore Entity Framework cannot create a direct-navigation (many-to-many) association between the tables. (Join tables with non-key fields are also known as join tables with payload.)
Create and use an audit table (ie GroupMembershipAudit consisting of
Group, User, join/unjoin, and DateTime
Actually you should create GroupMembershipAudit entity. With Code First table will be generated, you don't need to create it manually.

Entity Framework : map duplicate tables to single entity at runtime?

I have a legacy database with a particular table -- I will call it ItemTable -- that can have billions of rows of data. To overcome database restrictions, we have decided to split the table into "silos" whenever the number of rows reaches 100,000,000. So, ItemTable will exist, then a procedure will run in the middle of the night to check the number of rows. If numberOfRows is > 100,000,000 then silo1_ItemTable will be created. Any Items added to the database from now on will be added to silo1_ItemTable (until it grows to big, then silo2_ItemTable will exist...)
ItemTable and silo1_ItemTable can be mapped to the same Item entity because the table structures are identical, but I am not sure how to set this mapping up at runtime, or how to specify the table name for my queries. All inserts should be added to the latest siloX_ItemTable, and all Reads should be from a specified siloX_ItemTable.
I have a separate siloTracker table that will give me the table name to insert/read the data from, but I am not sure how I can use this with entity framework...
Thoughts?
You could try to use the Entity Inheritance to get this. So you have a base class which has all the fields mapped to ItemTable and then you have descendant classes that inherit from ItemTable entity and is mapped to the silo tables in the db. Every time you create a new silo you create a new entity mapped to that silo table.
[Table("ItemTable")]
public class Item
{
//All the fields in the table goes here
}
[Table("silo1_ItemTable")]
public class Silo1Item : Item
{
}
[Table("silo2_ItemTable")]
public class Silo2Item : Item
{
}
You can find more information on this here
Other option is to create a view that creates a union of all those table and map your entity to that view.
As mentioned in my comment, to solve this problem I am using the SQLQuery method that is exposed by DBSet. Since all my item tables have the exact same schema, I can use the SQLQuery to define my own query and I can pass in the name of the table to the query. Tested on my system and it is working well.
See this link for an explanation of running raw queries with entity framework:
EF raw query documentation
If anyone has a better way to solve my question, please leave a comment.
[UPDATE]
I agree that stored procedures are also a great option, but for some reason my management is very resistant to make any changes to our database. It is easier for me (and our customers) to put the sql in code and acknowledge the fact that there is raw sql. At least I can hide it from the other layers rather easily.
[/UPDATE]
Possible solution for this problem may be using context initialization with DbCompiledModel param:
var builder = new DbModelBuilder(DbModelBuilderVersion.V6_0);
builder.Configurations.Add(new EntityTypeConfiguration<EntityName>());
builder.Entity<EntityName>().ToTable("TableNameDefinedInRuntime");
var dynamicContext = new MyDbContext(builder.Build(context.Database.Connection).Compile());
For some reason in EF6 it fails on second table request, but mapping inside context looks correct on the moment of execution.