Linq to Entities: .Clear() does not delete relation row, only relation column value - linq

I have a 'users' and 'notifications' tables.
The tables definition are:
users notifications
------- -------------
ID (PK) ID (PK)
Name Name
users_ID (FK / Relation)
When I do:
MyUserEntity.notifications.clear();
MyContext.SaveChanges();
The users_ID column value(s) for the corresponding user entity are set to NULL, but I want the rows to be deleted.
What am I missing?
Thanks!

Try
foreach(var notification in MyUserEntity.notifications.ToList())
{
MyContext.DeleteObject(notification);
}
MyContext.SaveChanges();

Related

Lavavel Eloquent: Relationships with 5 table

I have an app consisting of these tables
users
id
name
user_location
id
user_id
cities_id
cities
id
state_id
country_id
name
states
id
country_id
name
countries
id
name
Then how to get relationship this five table?
you have to create a many-to-many relationship between tables and use pivot table to show the relationship between table rows using foreignId

Pivot table but not using table id

Is it possible to make a Pivot Table without using table id?
users
id
biometric_id
first_name
last_name
attendances
id
biometric_id
date
emp_in
emp_out
user_attendances
user_id
attendances_biometrics_id
I wanted to ask if this is available to link it like this? Because I need to show the attendance of the user that has his biometrics.
If it is possible, how?
If attendances.biometric_id has a unique constraint on it then there should be no reason why you cannot use it as a foreign key constraint.
Assuming your tables have been setup properly with foreign key constraints, your user model would probably have something like this:
public function attendances() {
return $this->belongsToMany('App\Attendances', 'user_attendances', 'user_id', 'attendances_biometrics_id');
}

Laravel 5.3 query to get results from 4 tables which has connections via Foreign Key

I'm using Laravel 5.3. I've 4 tables.
Default Users table. Departments, Position, Employees tables.
Users table has ID | Email | Password
Departments table has ID | Department | User_Id - Here User_Id is foreign key comes from Users table's ID
Positions table has ID | Position | Department_Id - Here Department_Id is foreign key comes from Departments table's ID
Employees table has ID | Employee | Position_Id - Here Position_Id is foreign key comes from Positions table's ID
User can have multiple Departments. Departments can have multiple Positions, Positions can have multiple Employees. So, if user is different, how can i retrieve all data from all 4 tables which that user had created?
You can use nested eager loading:
$departments = Department::where('user_id', $id)
->with('positions', 'positions.employees')
->get();
Another way is to build simple queries:
$departments = Department::where('user_id', $id)->get();
$positions = Position::whereIn('department_id', $departments->pluck('id'));
$employees = Employee::whereIn('position_id', $positions->pluck('id'));

Save on different tables using Eloquent Laravel Relationships

Hi I have 3 tables "temp_quotes", "quotes" and "quote_items"
Every quote generated goes to the "temp_quotes" table at first.
At the end of my multi-step form I need the following:
1- Receive all the quotes from the "temp_quotes" table, send it to the "quote_items" table and delete it from the "temp_quotes" table
2- Generate a new entry in the "quotes" table referencing the "users" table and
the "quote_items" table
Here is all the data and how needs to be save/removed on the tables:
temp_quotes Table
ID
BRAND
MODEL
YEAR
quotes Table:
ID
USER_ID
QUOTE_ID
quote_items Table:
ID
QUOTE_ID
BRAND
MODEL
YEAR
Any help will be appreciated.

inserting data in foreign key table in joomla2.5

I have a table
tbl_group
----------
groupId (pk)
groupName
and another table
tbl_group_head
--------------
groupHeadId (pk)
groupId (fk)
designation
I have already inserted data in tbl_group like 1002,cs and 1003,mca.
How to insert in tbl_group_head in joomla(2.5)?

Resources