Questions tagged [relationship]

Questions in this tag typically involve the association that a set of data has with other set(s) of data.

5577 questions
11
votes
2 answers

Laravel relationship not working: getConnectionName() Error

I have two tables users and user_details. I have linked users table as public function userDetails() { return $this->hasOne('App\Repositories\Models\UserDetails', 'id', 'user_id'); } and linked user_details table as public function user() { …
Tijan Manandhar
  • 322
  • 3
  • 18
11
votes
3 answers

Laravel eloquent get the latest row of related table

I have two tables, books, and chapters. One book has many chapters. Book model: public function chapters() { return $this->hasMany(Chapter::class); } Chapter model: public function book() { return $this->belongsTo(Book::class); } I want to…
trinvh
  • 1,500
  • 2
  • 11
  • 20
11
votes
2 answers

How to use "group by" with a polymorphic relationship in Laravel?

I have the following relationship: return $this->morphedByMany('App\Models\Movie', 'listable', 'cnt_lists', 'list_id')->withPivot('id', 'order', 'updated_at')->where('movies.has_poster', true)->orderBy('order', 'desc')->withTimestamps(); That…
ATLChris
  • 3,198
  • 7
  • 39
  • 65
11
votes
1 answer

Symfony/Doctrine relation COUNT in entity

As the title states, I would like to run 1 query to get results from a table with the count of their respective relationships. Lets say I have a Person entity with a OneToMany relationship with a Friend entity The Person entity might look something…
RVandersteen
  • 2,067
  • 2
  • 25
  • 46
11
votes
8 answers

Laravel whereHas across two separate databases

Problem: I want to get Customers only if they have already placed an order for the current year. Customers and Orders are on two separate Databases (this can't change). The relationships are all setup and working correctly but when I try the…
Pedro
  • 1,148
  • 4
  • 16
  • 35
11
votes
1 answer

How can I stop the movement of the nodes in Neo4j?

I'm using Neo4j with webadmin interface. When I run a query I display the nodes and relationships graphically. However, whenever I move a node, the other nodes connected to it move too. How can I stop this "force-attraction" effect? Thank you very…
RioMario
  • 121
  • 1
  • 5
11
votes
4 answers

CoreData transient relationship example

Does anybody have an example on how to model and code a transient to-one relationship in CoreData? For example, I have 2 entities with a one-to-many relationship. Doctor and Appointment. Now I want an transient relationship called…
batkuip
  • 1,480
  • 3
  • 15
  • 25
11
votes
3 answers

Doctrine2: OneToMany on mapped superclass

My DB structure is as follows: work: CTI table Work MappedSuperclass table AbstractImageWork which extends Work final table PhotoWork which extends AbstractImageWork comment: MappedSuperclass table Comment final table WorkComment which extends…
10
votes
1 answer

Django admin: Inline straight to second-level relationship

I have a three-levels Invoice model which I'd like to display on Django's admin area... in a "kind of special" way. Allow me to provide a bit of background: Each Invoice is conformed by several SubInvoice(s), and each SubInvoice is conformed by…
Savir
  • 17,568
  • 15
  • 82
  • 136
10
votes
1 answer

Laravel - Having a hasMany() as well as a hasOne() relationship on the same model?

I've got a Company model in which I would like to define two relationships with the User model: public function users(){ return $this->hasMany('App\User'); } public function administrator(){ return $this->hasOne('App\User',…
omrakhur
  • 1,362
  • 2
  • 24
  • 48
10
votes
1 answer

What is the rule to create a polymorphic field name in Laravel?

I'm not new using Laravel but it's first time that I need/want to use polymorphic relations. I saw a example at Laravel.com and it says that I should use 'imageable_id' to a table called 'images', in another example I saw 'commentable_id' to table…
10
votes
4 answers

Laravel Eloquent - firstOrCreate() on a relationship

When I try the firstOrCreate() on a relationship of another model, it does not work: Client::find($id)->users()->firstOrCreate(array('email' => $email)); This returns an error saying Call to undefined method…
Kousha
  • 32,871
  • 51
  • 172
  • 296
10
votes
4 answers

Relationships between classes that implement same interface

I'm pretty much java beginner, and i've come to ask stackoverflow forum for one simple question. I already checked similar question here on SO, but i didn't found the desired answer, LINK: Is there any relation between the class that implements…
rootpanthera
  • 2,731
  • 10
  • 33
  • 65
10
votes
2 answers

Sqlalchemy: secondary relationship update

I have two tables, say A and B. Both have a primary key id. They have a many-to-many relationship, SEC. SEC = Table('sec', Base.metadata, Column('a_id', Integer, ForeignKey('A.id'), primary_key=True, nullable=False), Column('b_id', Integer,…
Sri
  • 405
  • 6
  • 16
9
votes
2 answers

Rails includes nested relations

I need to query all posts from a specific user and include all comments and the user who belongs to the comment. class User < ... has_many :posts has_many :comments end class Post < ... belongs_to :user has_many :comments end class Comment…
sandelius
  • 507
  • 2
  • 6
  • 13