relationship

Laravel eloquent get the latest row of related table

冷暖自知 提交于 2020-05-13 06:06:24
问题 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 get book list with their own latest chapter using single query like this: $books = Book::with(['authors', 'categories', 'chapters' => function($q) { $q->orderBy('updated_at', 'desc')->first(); }]->get(); But it doesn't work. Chapters return an empty array

Implementing Composition in Java

此生再无相见时 提交于 2020-05-13 04:18:22
问题 class Book { private Chapter[] chapters = new Chapter[5]; } class Chapter { private Book book; } Is this the correct way to implement the above relationship? I need explanation on this. Thanks. 回答1: That is not enough. In Composition relationship, If whole instance is destroyed, the part instance should also be destroyed immediately . You should have some codes (some machanisms) to do that. For example if you push the instances of Chapter s from external the class (for example by using a

Implementing Composition in Java

可紊 提交于 2020-05-13 04:18:08
问题 class Book { private Chapter[] chapters = new Chapter[5]; } class Chapter { private Book book; } Is this the correct way to implement the above relationship? I need explanation on this. Thanks. 回答1: That is not enough. In Composition relationship, If whole instance is destroyed, the part instance should also be destroyed immediately . You should have some codes (some machanisms) to do that. For example if you push the instances of Chapter s from external the class (for example by using a

How to set value for additional column in pivot table in OctoberCMS?

筅森魡賤 提交于 2020-04-30 07:07:45
问题 I have doctor and specialization table, and have doctor_specialization_pivot table. In my pivot table I have the following columns: | doctor_id | additional_data | specialization_id | additional_data is from the doctor model along with the doctor_id . In my doctor model file, I have this relationship: public $belongsToMany = [ 'specialization' => [ 'path\to\specialization\model', 'table' => 'doctor_specialization_pivot', 'parentKey' => 'doctor_id', 'otherKey' => 'specialization_id', ] ]; Now

laravel eloquent relationship polymorphic

╄→гoц情女王★ 提交于 2020-04-17 20:37:12
问题 I have a polymorphic relationship, comments: (id, text, commentable_id, commentable_type ), images :(id, title) and videos :(id, title). videos table has a many to many relationship with matters table. I managed to recover the comments and their relationship such as the video but I cannot recover the title of the matter, passing from the comments table, after the videos table and then table of matters. below my request. class Comment extends Model { public function commentable() { return

Get all relations name in laravel5 model

↘锁芯ラ 提交于 2020-04-17 07:12:58
问题 I have model include of several relation for example like this: public function NewsCategory() { return $this->belongsTo("News\Model\NewsCategory"); } public function NewsImage() { return $this->belongsTo("News\Model\NewsImage"); } public function NewsTag() { return $this->belongsTo("News\Model\NewsTag"); } and relations create dynamically. How can I get all this class name? In this example I want NewsCategory,NewsImage,NewsTag 回答1: one approach is to as follows : $results = ModelClass::where

Siblings relationship between same models in Vapor

人盡茶涼 提交于 2020-04-13 17:15:40
问题 I have a User model which I want to add a friends property to. Friends, are supposed to be other User s. I created the UserFriendsPivot : final class UserFriendsPivot: MySQLPivot, ModifiablePivot { var id: Int? var userID: User.ID var friendID: User.ID typealias Left = User typealias Right = User static var leftIDKey: WritableKeyPath<UserFriendsPivot, Int> { return \.userID } static var rightIDKey: WritableKeyPath<UserFriendsPivot, Int> { return \.friendID } init(_ user: User, _ friend: User)

Create a SQL table that can have cascading child parent relationships

喜夏-厌秋 提交于 2020-03-05 00:27:37
问题 I have a set of entities that can have child entities which themselves have child entities and they have child entities .... etc. The problem is that the number of subsequent child entities is not constant between entities. Example: Car has an engine, the engine have multiple components which may comprise multiple parts and the parts my have smaller parts As stated above, the number of subsequent child entities cascading down is different for instance between a Train and a Car, so I cannot

ER-Diagram: Ternary Relationship - How to read properly?

一个人想着一个人 提交于 2020-02-17 07:37:10
问题 Im not quite sure how to read ternary relationships within a ER-Diagram. Lets say this is the ternary relationship that is given. What can I interpret out of that? It says that you have to put your hand on 2 entity sets and then read it like that. Hand on Account and User: A pair of Account and User can be associated with M projects. Hand on Account and Project: A pair of Account and Project can be associated with M users. Hand on Project and User: A pair of Project and User can be associated

Sqlalchemy include empty relationship with join and contains_eager

删除回忆录丶 提交于 2020-01-30 12:24:06
问题 I have 2 models Recording and Recording_results like so class Recording(Base): __tablename__ = 'recordings' id = Column(Integer, primary_key=True) filename = Column(String, nullable=False) language_id = Column(Integer, ForeignKey('languages.id'), nullable=False) language = relationship("Language", back_populates="recordings") user_id = Column(Integer, ForeignKey('users.id'), nullable=False) user = relationship("User", back_populates="recordings") created_at = Column(DateTime, nullable=False,