V
V
Victor2015-12-21 22:53:37
ORM
Victor, 2015-12-21 22:53:37

How to get information about relationships in Laravel?

Hello.
Is there a way in Laravel for a particular model to get information about its relationships?
For example, in the form of such an array:

[
            "hasOne" => [
                "about" => [
                    "model" => "UserAbout",
                    "foreign_key" => "user_id"
                ]
            ],
            "belongsTo" => [
                "company" => [
                    "model" => "Company",
                    "foreign_key" => "company_id"
                ]
            ],
            
            "hasMany" => [
                "posts" => [
                    "model" => "Post",
                    "foreign_key" => "user_id"
                ]
            ]
        ];

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
D', 2015-12-22
@Denormalization

No.
If there is no explicit indication (like $with = [];), then there is no way to find out.

A
Andrzej Wielski, 2015-12-22
@wielski

No specific link information can be obtained.
What exactly do you need it for?
If for optimization - put laravel-debugbar, it will help to have an idea about the requests on a particular page, and have a rough idea of ​​​​the links. (if you understand sql syntax, of course)

V
Vyacheslav Plisko, 2015-12-22
@AmdY

The binding happens at runtime
public function items() { return $this->hasOne(....); } and only at the moment of calling ->items the model learns about it,
therefore it is technically impossible to trace the links

D
Dmitry Evgrafovich, 2015-12-22
@Tantacula

If you need already loaded relationships, then $model->getRelations(), and about saving the model along with the relationships, see the push() method. The rest you've been told.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question