staudenmeir / eloquent-has-many-deep Goto Github PK
View Code? Open in Web Editor NEWLaravel Eloquent HasManyThrough relationships with unlimited levels
License: MIT License
Laravel Eloquent HasManyThrough relationships with unlimited levels
License: MIT License
Given the relationship:
Country
→ has many to many → User
→ has many to many → Post
Can we get: all Posts
of all Users
in a Country
Hello,
I'm having following models and table name:
Tags
: table name (project_tags
)
id
name
which is related to TechnicalDescription
model with many to many
relationship:
TechnicalDescription
: table name(project_technical_detail
)
id
project_id
description
Many to many
relation holds on table (technical_detail_tag_relation
):
id
technical_detail_id
tag_id
And finally I have Project
Model table name(projects
)
id
name
So I have Tags
-> belongsToMany
-> TechnicalDescription
-> belongsTo
-> Project
I tried doing:
public function projects()
{
return $this->hasManyDeep(
Project::class,
['technical_detail_tag_relation', TechnicalDescription::class],
[null, null, 'id'],
[null, null, 'project_id']
);
}
But it throws error:
Column not found: 1054 Unknown column 'technical_detail_tag_relation.technical_description_id' in 'on clause'
Can you help me out with it, I tried replacing technical_description_id
almost every null
place where I defined this relationship, but no luck.
I'm trying to learn about your package, but little confused with this kind of relationships.
Thanks.
Is this possible using this package?
User
→ hasMany → Campaign
→ belongsTo → Template
.
I need $user->templates
.
now I use:
public function getTemplatesAttribute()
{
return $this->campaigns->map->template;
}
But this is just a Collection, not relationship.
Thanks!
Is there a way to use the whereHas
method properly with a hasOneDeep
instance?
The pivot table:
order_id | status_id | step | completed_at |
---|---|---|---|
1 | 1 | 10 | 2019-0706 16:00 |
1 | 2 | 20 | null |
1 | 3 | 30 | null |
1 | 7 | 40 | null |
1 | 5 | 50 | null |
I use the relationship like:
public function lastCompletedStatus()
{
return $this->hasOneDeep(Status::class, [OrderStatus::class])
->where('completed_at', '!=', null)
->latest('step');
}
But if I filter through this relation:
$orders = $orders->whereHas('lastCompletedStatus', function($query){
$query->where('description', 'order_checked');
});
It gives me a collection with all orders completed the given status. But I want to filter for orders in this specific status. Is this even possible?
Hi Dear,
I have these relations:
Article->(hasMany)Translate->(belongTo)translator
I need to Article->translators() and reverse
thanks
This was already brought up in #22, but that ticket went in a different direction. Can table aliases be used with hasManyDeepFromRelations
?
Hi,
I have this example in the Address
model
Address
has BillingAccount
through Account
Address -> Account -> BillingAccount
However, the ID return on the Address array is of the Account array instead.
public function address_with_billing_account()
{
return $this->hasManyDeep(
BillingAccount::class,
[
Account::class,
],
[
'id', //id of account in account table
'id', //id of billing account in billing account table
],
[
'account_id', //id of account in address table
'billing_account_id', //id of billing account in account table
]
);
// equivalent:
// return $this->account->billing_account();
}
It seems to be related to the fact that joins is being used, but then a select query used is of the Account id, that's why Address id gets overridden (because same column name)
Has anyone faced this issue?
Thanks
I have global scopes that are applied in the boot() method of some of my models in a HasManyDeep relationship.
It seems like these are being ignored - in effect, I'm getting data that I shouldn't be because my model 'active' property is being ignored.
Is this expected behaviour, or should global scopes apply in intermediate models?
how can i write query builder on this package for each spesfic model like where clause for columuns
I think there's a typo, otherwise I didn't catch the belongsToMany
paragraph
Its written
public function permissions() { return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role']); }
Shouldn't it be
public function permissions() { return $this->belongsToMany('App\Permission', ['role_user', 'App\Role']); }
?
Hello, i have hasManyDeep relation using table aliases and in result i have my result model columns and one column of one pivot tables.
Like:
select
vertices
.*,head_vertices
.user_id
from
head_vertices.user_id is not a part of result model columns.
If i try to add select('vertices.') to relation, it doesn't change anything. The only way to hide this column is use getQuery()->select('vertices.'). But in this case i get a Builder instance instead a relationship instance.
Do any of your relationships allow for a merge of a belongsTo
and a hasOneDeep
, like:
ifnull(belongsTo,hasOneDeep)
Currently:
public function locationPaymentMethod(){
return $this->belongsTo('App\Models\PaymentMethod','payment_method_id');
}
public function paymentMethod(){
// get this location.paymentMethod or the location.company.paymentMethod
return $this->locationPaymentMethod ? $this->locationPaymentMethod() : $this->hasOneDeep('App\Models\PaymentMethod',['App\Models\Location', 'App\Models\Company'],['id','id','id'],['id','company_id','payment_method_id'])->latest();
}
I'm looking to eliminate that ternary check when getting Location::with('paymentMethod')->get()
So, I have this type of relationship:
ApplicationType
-> Many To Many -> Page
-> Many To Many -> Section
-> Many To Many -> Question
My relationship with hasManyDeep
:
/**
* @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep|Question
*/
public function questions()
{
return $this->hasManyDeep(Question::class,
[
'application_types_pages',
Page::class,
'pages_sections',
Section::class,
'sections_questions'
]
);
}
This one returns []
and it should return 3 objects.
This is my corresponding join statements:
ApplicationType
::whereId(1)
->join('application_types_pages', 'application_types.id', '=', 'application_types_pages.application_type_id')
->join('pages', 'application_types_pages.page_id', '=', 'pages.id')
->join('pages_sections', 'pages.id', '=', 'pages_sections.page_id')
->join('sections', 'pages_sections.section_id', '=', 'sections.id')
->join('sections_questions', 'sections.id', '=', 'sections_questions.section_id')
->join('questions', 'sections_questions.question_id', '=', 'questions.id')
->get();
This one returns everything as intended but I hate joins as it it returning messy single layer array.
Am I doing anything wrong?
i have 3 table and want to make relation between them
order:
id,total
order_details:
order_id
dish_name
extra_items
id
title
order_details_extra_items
order_details_id
extra_items_id
how can make relation between them in Order Model؟
i try to use this function
public function order_details_extra()
{
return $this->hasManyDeep(
'App\Models\OrderDetails',
['order_details_extra_items', 'App\Models\ExtraItems'],
[
'order_details_id',
'id',
'order_id'
],
[
'id',
'extra_items_id',
'id'
]
);
}
Nice Packages @staudenmeir,
I have an issue.
having following tables
country table
id | name | code |
---|---|---|
int | string | string |
city table
id | country_id | name |
---|---|---|
int | int | string |
University table
id | city_id | name | other_columns |
---|---|---|---|
int | int | string | type |
I'm Filtering According To The Country And Other Column On The University Table
.
The Issue Here Is University table
only hascity_id
but I Want Use University table
instead of the city table
How I can acheive that with hasOneDeep
Hi, I have relationships like this:
User → belongsToMany Product → BelongsTo Customer
User Table: user.id
Product Table :
---- product.id
---- product.customer_id
Pivot table:
---- product_user.user_id
---- product_user.product_id
Customer Table:
customer.id
Does eloquent-has-many-deep support this? I only found this similar but for morphed by many:
Tag → morphed by many → Post → belongs to → User
How can I get user->customer()
and reverse: customer->user()
?
Thank you!
I'm not sure, that is that package functionality, but it would be great, if you add ability to use composite keys on creating relations.
As example:
SELECT
a.*
FROM
a_table AS a
INNER JOIN
b_table AS b
ON
a.name = b.name
AND
a.surname = b.surname
Hi!
If I have something like the following:
class Post extends Model
{
public function similar()
{
return $this->hasManyDeep(
self::class,
['taggable as alias', Tag::class, 'taggable']
);
}
}
The following sql is generated:
select "posts".*, "taggable" as "alias.post_post_pk"
from "posts"
inner join "taggable" on "taggable"."post_post_pk" = "posts"."post_pk"
inner join "tags" on "tags"."tag_pk" = "taggable"."tag_tag_pk"
inner join "taggable" as "alias" on "taggable" as "alias.tag_tag_pk" = "tags"."tag_pk"
where "taggable" as "alias.post_post_pk" = 1
As you can see the "taggable" as "alias.tag_tag_pk"
is the problematic part.
The error first occurs in HasManyDeep:110
as inside Model::qualifyColumn
$this->getTable()
returns project_category as alias
. Then $this->getQualifiedFirstKeyName()
returns the same so when the where is added this generates the wrong alias.
Edit: Example is replaced with Models from the test of this package.
I have 2 hasManyDeep relations, where i use table aliases and both work great. But if i try to create new relation from this two via hasManyDeepFromRelation, table aliases disappear from query and i get an error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias.
Is it possible to add table aliases to hasManyDeepFromRelation?
Is this possible using this package?
Event
→ morpthToMany → Product
→ morphMany → OrderItem
Thank you
Hello,
I'm a bit new to Eloquent and I'm still learning how to use it. Would the following be possible with your extension?
https://laracasts.com/discuss/channels/eloquent/multiple-layers?page=1#reply=509652
Much appreciated, regards,
Bastiaan
Hello, me again... (sorry!)
I currently have have the following relation setup on my Collective
model (as per #2)
public function videos()
{
return $this->hasManyDeep(Video::class, [Artist::class, 'artist_video'])->groupBy('videos.id');
}
How can I query this relation? I'm currently trying the following:
$collective->videos()->where('slug', 'my-slug')->first();
However I'm receiving the following error:
Integrity constraint violation: 1052 Column 'slug' in where clause is ambiguous (SQL: select
videos
.*,artists
.collective_id
fromvideos
inner joinartist_video
onartist_video
.video_id
=videos
.id
inner joinartists
onartists
.id
=artist_video
.artist_id
whereartists
.collective_id
= 1 andslug
= my-slug group byvideos
.id
limit 1)
Any ideas how I can get this to work?
Thanks in advance!
The filtering is working as expected but, is there any way to eager load relationships for a intermediate/pivot table?
public function teachers()
{
return $this->hasManyDeepFromRelations($this->enrolments(), (new Enrolment())->user())
->withIntermediate(Enrolment::class)
->where('request_status', 'Approved') //Filter by Enrolment column
->with('school') //Enrolment model relationship (Doesn't work)
->with('enrolment.school') //I have also tried this (Doesn't work)
->with('courses') //User model relationship (Works)
->whereHas('roles', function ($query) { //Filter by user.roles column
$query->where('name', 'teacher');
});
}
I tried this as well but I see that hasManyDeepFromRelations doesn't preserve additional constraints. #36 (comment)
public function enrolments()
{
return $this->hasManyThrough(
Enrolment::class,
CourseSchool::class,
'course_id',
'course_school_id'
)
->with('school'); //Also doesn't work
}
Not sure if this is intentional, but since the README on 1.8 still says "supports laravel 5.5.29+", I'm guessing it's not.
I have laravel/framework locked to 5.5.* in my composer file (5.5 is LTS), and when attempting to install 1.8 I get a conflict due to the laravel requirements in your composer.json.
Output follows
# composer require staudenmeir/eloquent-has-many-deep:"^1.8"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for staudenmeir/eloquent-has-many-deep ^1.8 -> satisfiable by staudenmeir/eloquent-has-many-deep[v1.8].
- Conclusion: remove laravel/framework v5.5.45
- Conclusion: don't install laravel/framework v5.5.45
- staudenmeir/eloquent-has-many-deep v1.8 requires illuminate/database 5.8.* -> satisfiable by illuminate/database[v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.2, v5.8.3, v5.8.4, v5.8.8, v5.8.9].
- don't install illuminate/database v5.8.0|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.11|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.12|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.14|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.15|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.2|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.3|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.4|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.8|don't install laravel/framework v5.5.45
- don't install illuminate/database v5.8.9|don't install laravel/framework v5.5.45
- Installation request for laravel/framework (locked at v5.5.45, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.45].
Installation failed, reverting ./composer.json to its original content.
I'm trying to using BelongsToThrough package with this package. But combining two of them it's not working. Maybe I'm missing something easy but anyway I want to get Orders -> Areas.
Order
→ belongs to → Address
→ belongs to → District
-> belongs to many -> Area
orders
id | other_columns | address_id |
---|---|---|
1 | Something | 1 |
addresses
id | other_columns | district_id |
---|---|---|
1 | Something | 1 |
districts
id | name |
---|---|
1 | Kadıkoy |
areas
id | name |
---|---|
1 | First Area |
area_district
area_id | district_id |
---|---|
1 | 1 |
In my Order model I have districts
relation
public function districts ()
{
return $this->belongsToThrough(District::class, Address::class);
}
When I add areas
relation like this;
public function areas ()
{
return $this->hasManyDeepFromRelations($this->districts(), (new District())->areas());
}
I got Exceptions because in your hasOneOrManyDeepRelationMethod
hasn't BelongsToThrough
Is there any way to use both? Or maybe I miss something and Can anyone help?
Thanks,
Can
I installed the package, but it does not work
Hi, does hasOneDeep()
supports HasEagerLimit
?
The docs said that it supports laravel's basic relationships HasOne
/HasOneThrough
/MorphOne
but I'm using your hasOneDeep
relationships.
Is it possible to use the window function from the hasOneDeep relationships?
Thank you
Hi, your work saved my life in terms of Eloquent Relationships everything that I needed was related to you on github 🥇
I wonder if it would be possible to have a hasManyDeepJson kind of hybrid like this :
class Mapregion extends Model { public function groups() { return $this->hasManyDeepJson('Models\Group', ['Models\Subregion', 'address->hq->country_id']); } }
thanks a lot
$ composer require staudenmeir/eloquent-has-many-deep
Using version ^1.6 for staudenmeir/eloquent-has-many-deep
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for staudenmeir/eloquent-has-many-deep ^1.6 -> satisfiable by staudenmeir/eloquent-has-many-deep[v1.6].
- Conclusion: remove laravel/framework v5.5.25
- Conclusion: don't install laravel/framework v5.5.25
- staudenmeir/eloquent-has-many-deep v1.6 requires illuminate/database ^5.5.29 -> satisfiable by illuminate/database[5.7.17, 5.7.18, 5.7.19, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9].
- don't install illuminate/database 5.7.17|don't install laravel/framework v5.5.25
- don't install illuminate/database 5.7.18|don't install laravel/framework v5.5.25
- don't install illuminate/database 5.7.19|don't install laravel/framework v5.5.25
- don't install illuminate/database v5.5.33|don't install laravel/framework v5.5.25
- don't install illuminate/database v5.5.34|don't install laravel/framework v5.5.25
- don't install illuminate/database v5.5.35|don't install laravel/framework v5.5.25
- don't install illuminate/database v5.5.36|don't install laravel/framework v5.5.25
- don't install illuminate/database v5.5.37|don't install laravel/framework v5.5.25
When working on a simple hasManyDeep() relation, I'm unable to save() a new relation.
I can see there's no InteractsWithPivotTable trait used by these relations.
Would it be compatible to add mutating queries inside InteractsWithPivotTable to your relationships?
hi, im using category table multiple level, for relation of grandpa(level 1) category with product use this package witch very well and work, but relation of father(level 2) category with product not work, this relation
public function throughProducts() { return $this->hasManyThrough(Category::class, 'App\Models\Category', 'parent_id', 'parent_id'); }
this error :
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'categories' (SQL: select count(*) as aggregate from
categories
inner joincategories
oncategories
.id
=categories
.parent_id
wherecategories
.parent_id
= 2)
Hello,
Excellent package! I currently have the following relations;
User → belongsToMany
→ Area → hasMany
→ Venue
Is there a way for me to get the users venues? $user->venues;
A venue can then have "entries".
Venue → hasMany
→ Entry
I would then like to get the users entries based on the above. Is this possible?
My end goal would be to be able to do this;
// fetch the users venues based on the areas they're assigned to
$venues = $user->venues;
// fetch the users entries based on the venues the entries are assigned to (the above needs to be done first perhaps?)
$entries = $user->entries;
Hope you can help!
I am using your package but the detach method for a deep relationship is not working for me.
There are any know problem with it?
Hi,
First of all, thank you for creating this extension. It's very useful and I think it should be added to Laravel.
My question: is it possible to combine MorphedByMany with BelongsTo?
Example: User
-> morphedByMany -> Account
-> belongsTo -> Partner
Currently I'm using this line to retrieve Partner
models:
$user->accounts()->with('partner')->withTrashed()->get()->pluck('partner')->unique()
I'm hoping there is a better way to do this.
Thank you in advance!
Hello!
At first, i'm really grateful to you because your package is really helpful, so thank you so much.
However, I'm a little confused when using custom keys in intermediate tables, that is because I have a relation App\User BelongsToMany App\User then the foreign keys couldn't be in the way as laravel require.
The structure of the tables are.
users :
id
responsible_user:
id - responsible_id - user_id
farms :
id
farm_user :
id - user_id - farm_id
the way that I need the relation is:
User -> BelongsToMany -> Users -> BelongsToMany -> Farms
I did something like:
public function farms(){
return $this->hasManyDeep('App\Farm', ['responsible_user','App\User','farm_user']);
}
but I need to define the custom keys to responsible_user pivot table.
Thank you for your attention and sorry for disturbing you.
Is this possible using this package?
Region
→ has many → Destination
→ belongs to many → Activity
I would like to get activities
for region
Thank you
Thank you for this wonderful package. I just came to know about it recently and it has already made many parts of my code simpler.
I went through the documentation but there's one problem where I would appreciate some help.
I have the following relationship:
Now I was able to get the companies of the lawyer office by using hasManyDeepFromRelations as
// Inside LawyerOffice Model
public function companies()
{
return $this->hasManyDeepFromRelations($this->lawyers(), (new Lawyer)->companies())->active();
}
But when I do the same for claims as
public function claims()
{
return $this->hasManyDeepFromRelations($this->companies(), (new Company)->claims());
}
It ignores the active() query scope of the companies and returns claims of all the companies.
I assume this is something related to #35 but I am not sure where should I add the query scope manually.
Can you please guide me on how can I add query scope or where() condition in order to get the claims of the active companies of a lawyer office.
Thank you.
Your extension looks very promising. However I'm not sure how it handles more levels of many-to-many relations.
Example: App\User
belongsToMany App\Group
belongsToMany App\Album
belongsToMany App\Photo
.
How would I get photos belonging to a user? I'm wondering how I would use the hasManyDeep()
method.
Scenario:
I have Institution -> Departments -> Qualifications -> Courses
and both Qualification
and Course
morph Group
. I want to load all groups for an institution.
I tried:
/**
* Get all of the Institution's groups.
*/
public function groups()
{
return $this->hasManyDeepFromRelations(
$this->qualificationsGroups(), $this->coursesGroups()
);
}
/**
* Get all of the Institution's qualifications' groups.
*/
public function qualificationsGroups()
{
return $this->hasManyDeep(
Group::class,
[Department::class . " as d1", Qualification::class . " as q1"],
[null, null, ['groupable_type', 'groupable_id']]
);
}
/**
* Get all of the Institution's courses' groups.
*/
public function coursesGroups()
{
return $this->hasManyDeep(
Group::class,
[Department::class . " as d2", Qualification::class . " as q2", Course::class],
[null, null, null, ['groupable_type', 'groupable_id']]
);
}
Issue:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'groups' (SQL: select count(*) as aggregate from groups
........
Question:
How do I resolve this or any other approach to loading groups for both courses and qualifications under an institution?
Assume the following entities:
User
UserGroup
Organization
Assume the following relationships:
User
belongs to many Organization
s.User
belongs to many UserGroup
s.Given above, we need a relationship User::organizationsOfUsersWithSharedUserGroups()
, that would return all the Organization
s that belongs to any of the User
s that shares UserGroup
s with the calling User
object.
PHP has to:
User
object,UserGroup
s,User
s of those UserGroup
s (Thus coming back to the User
class, through the same pivot table.),Organization
s of those User
s.Following relationship definition accomplishes what I am after, with one caveat:
public function organizationsOfUsersWithSharedUserGroups() {
return $this->hasManyDeep(
Organization::class,
[
'user_user_group',
UserGroup::class,
'user_user_group2',
User::class,
'organization_user',
]
);
}
The caveat being user_user_group2
being a MySQL view defined as follows:
CREATE
OR REPLACE
VIEW `db`.`user_user_group2` AS select
`db`.`user_user_group`.`id` AS `id`,
`db`.`user_user_group`.`user_id` AS `user_id`,
`db`.`user_user_group`.`user_group_id` AS `user_group_id`
from
`db`.`user_user_group`
Table user_user_group
and view user_user_group2
are identical.
If we replace user_user_group2
with user_user_group
in User::organizationsOfUsersWithSharedUserGroups()
's definition, we get the following error:
SQLSTATE[42000]: Syntax error or access violation:
1066 Not unique table/alias: 'user_user_group'
(SQL:
select
`organizations`.*,
`user_user_group`.`user_id` as `laravel_through_key`
from
`organizations`
inner join `account_user`
on `account_user`.`account_id` = `organizations`.`id`
inner join `users`
on `users`.`id` = `account_user`.`user_id`
inner join `user_user_group`
on `user_user_group`.`user_id` = `users`.`id`
inner join `user_groups`
on `user_groups`.`id` = `user_user_group`.`user_group_id`
inner join `user_user_group`
on `user_user_group`.`user_group_id` = `user_groups`.`id`
where
`users`.`deleted_at` is null
and `user_groups`.`deleted_at` is null
and `user_user_group`.`user_id` = 1
and `organizations`.`deleted_at` is null
)
I tried to specify an as
in User::organizationsOfUsersWithSharedUserGroups()
's definition as follows, but it did not work.
public function organizationsOfUsersWithSharedUserGroups() {
return $this->hasManyDeep(
Organization::class,
[
'user_user_group',
UserGroup::class,
'user_user_group as alias',
User::class,
'organization_user',
]
);
}
Thus, I would like to propose supporting table aliases with BelongsToMany relationships.
class BroadActivity extends Model
{
use HasRelationships;
public function detailActivities()
{
return $this->hasMany(DetailActivity::class);
}
public function targets()
{
// How to relate the Targets with pivot value (target_value)?
}
}
class DetailActivity extends Model
{
public function broadActivity()
{
return $this->belongsTo(BroadActivity::class);
}
public function targets()
{
return $this->belongsToMany(Target::class)->withPivot('target_value');
}
}
class Target extends Model
{
public function detailActivities()
{
return $this->belongsToMany(DetailActivity::class);
}
}
How to create a relation in BroadActivity model to Target with additional pivot values?
First of all, thanks for try to make our lifes easier! This is a very good solution. :)
Give me a little help?
I have the following eloquent entities:
where a MovieTheater hasMany MovieTheaterRooms, each MovieTheaterRoom hasMany MovieSessions and each MovieSession belongsTo a Movie.
What I'm trying, is to get all Movies that are playing today on a MovieTheater by its $movieTheaterId
, but since it is a long relation, I'm unable of retrieve such collection.
This is what I'have tried:
class MovieTheater extends Model {
use HasRelationships;
public function movies()
{
return $this->hasManyDeep(Movie::class, [MovieSession::class, MovieTheaterRoom::class]);
}
}
And when calling App\Models\MovieTheater::find(1)->movies()->get()
This is the output:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'movie_sessions.movie_theater_id' in 'field list' (SQL: select
movies
.*,movie_sessions
.movie_theater_id
frommovies
inner joinmovie_theater_rooms
onmovie_theater_rooms
.id
=movies
.movie_theater_room_id
inner joinmovie_sessions
onmovie_sessions
.id
=movie_theater_rooms
.movie_session_id
wheremovie_sessions
.movie_theater_id
= 1)'
Where I'm wrong?
Hi
I've successfully implemented this Laravel package, it's awesome
I have two possible Relations:
Order -> belongs to many -> DeliveryOrder -> belongs to many -> Invoices
Order -> belongs to many -> Invoices
Some Orders
can relate directly to Invoices
, some will have to go through DeliveryOrder
They have different possible paths but the start and end Models of the possible paths are the same
I'm able to use hasManyDeep to get both relations, separately
But how can we add support or workaround to combine the two Relations into one?
(not asking to convert into a Collection or array and merge them, that's cheating)
Thanks!
Hello,
I currently have the following relations;
Collective → hasMany
→ Artist → belongsToMany
→ Video
I currently have the following hasManyDeep
relation setup for a collective to fetch their videos.
public function videos()
{
return $this->hasManyDeep(Video::class, [Artist::class, 'artist_video']);
}
However, when I fetch the videos for a collective using $collective->videos
I receive duplicate videos since an the same artists in a collective, can be assigned to the same video.
How can I ensure that only unique videos are fetched? Is there something wrong with my relation?
Hi,
I have a relationship defined as:
User
→ morphToMany Team
-> morphTo -> Campaign
I'm trying to get a list of all campaign
s that a User
belongs to. Looking at the documentation I think I should be able to do something like:
return $this->hasManyDeepFromRelations(
$this->rolesTeams(),
(new Team)->teamable()
);
But that throws the following error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'teams' (SQL: select count(*) as aggregate from `teams` inner join `teams` on `teams`.`teamable_id` = `teams`.`team_id` inner join `role_user` on `role_user`.`team_id` = `teams`.`id` where `role_user`.`user_id` = 8 and `role_user`.`user_type` = App\User)
My User
model looks something like this:
class User extends Authenticatable implements MustVerifyEmail
{
...
/**
* Many-to-Many relations with Team associated through the roles.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function rolesTeams()
{
return $this->morphToMany(
App\Team::class,
'user',
'role_user',
'user_id',
'team_id'
)
->withPivot('role_id');
}
/** Get the campaigns that the user is a member of
*
* @return Builder
*/
public function campaigns()
{
return $this->hasManyDeepFromRelations(
$this->rolesTeams(),
(new Team)->teamable()
);
}
...
My Team
model is like:
class Team extends LaratrustTeam
{
...
/**
* Get all of the owning teamable models.
*/
public function teamable()
{
return $this->morphTo();
}
...
}
Any idea what I'm doing wrong here?
i have a parent->student->assignments->tutor..
now parent/student Model while separate (extending USER Model) use same users table with type_id.. and has a parent_id which signifies whether a row has a parent...
i tried doing:
{
// Country → has many → User → has many → Post → has many → Comment
return $this->hasManyDeep(Tutor::class, [Student::class, 'assignments'], [
'parent_id', // Foreign key on the "users" table.
'student_id', // Foreign key on the "posts" table.
'tutor_id' // Foreign key on the "comments" table.
]);
}
on my Client model but it returns:
Illuminate/Database/QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users' (SQL: select `users`.*, `users`.`parent_id` from `users` inner join `assignments` on `assignments`.`id` = `users`.`tutor_id` inner join `users` on `users`.`id` = `assignments`.`student_id` where `users`.`parent_id` = 1 and `type_id` = 4)'
This looks like an amazing library and exactly what I need. However, I've literally read the examples 50 times trying to figure out how to accomplish what I need, but I just can't understand it. First of all, does this library work for my situation? And if so, what is the proper relationship definition?
I'm have the following relationship:
Product -> belongsTo -> SSIN -> belongsToMany -> stockableItems
I am trying to set up a stockableItems
relationship on the Product
model.
I can not for the life of me figure out the right way to do this. Can anyone help? Here is something I tried, using the advanced hasManyDeepFromRelations
method:
public function stockableItems() {
return $this->hasManyDeepFromRelations(
$this->ssin(),
(new Ssin)->stockableItems()
);
}
This seemed to work, but then I noticed that when I retrieved $product->stockableItems
, the stockableItems were actually being returned with the wrong ID. For some reason they were being assigned an ID equal to the ssin_id
from the ssin_stockable_item
join table.
I'm not sure if this is a bug or if my setup is wrong. Also, I probably don't even need to use the hasManyDeepFromRelations
method, but as stated above, I can't figure out from the documentation how this relationship is supposed to be set up.
Hi!
This library works like a charm! The only issue I currently have is that I'd like to define the reverse of the HasManyDeep relation, so in your example the Comment
model having a singular Country
. Would it be possible to add this relationship as well?
Cheers
Hi,
i have the problem in object.
How can i solve it?
This is my code:
class Product extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
.....
Hi @staudenmeir
I really appreciate the repo
Out of desperation, I've moved a lot of advanced relationships to depend on MySQL's View instead
Creating a fake table, to trick laravel, eager loading, etc has been working
Have been depending on subquery joins lately
also, users should be discouraged to use appends()
and use leftJoin()
instead
I guess Laravel is built without having deep relationships in mind, which leaves out a lot of functions, causing a lot of limitations
Now I can even union relationships, adding where clauses for the relationships etc.
Would be nice if I knew earlier that Laravel's not capable and MySQL's View is there to help
If I'm right, please add a note to point out MySQL's View is the way to go if the relations are to advanced
In fact, I think Laravel should allow Migrations to create virtual tables
Part of my code that creates a fake relationship, based on the distance of an Address's GPS, to a Polygon of the ServiceArea table. The distance must be less than 0.05
$query = DB::query()->selectRaw("
#SELECT
*
FROM
(
SELECT
`crm_addresses`.`id` AS `address_id`,
`crm_servicearea`.`id` AS `service_area_id`,
`crm_addresses`.`updated_at` AS `updated_at`,
`crm_addresses`.`created_at` AS `created_at`,
FORMAT(ST_DISTANCE(
`crm_servicearea`.`polygon`,
`crm_addresses`.`gps`
), 6) AS `distance`
FROM
`crm_addresses`
INNER JOIN `crm_servicearea` WHERE `crm_addresses`.`gps` IS NOT NULL
ORDER BY
`address_id`,
`distance`
) AS address_service_area
WHERE
`distance` <= 0.05
");
}
then:
$query_string = getSqlString$query);
$sql = "create view `{$pivot_table_name}` as {$query_string}";
DB::statement($sql);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.