Answer the question
In order to leave comments, you need to log in
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
No.
If there is no explicit indication (like $with = [];), then there is no way to find out.
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)
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
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 questionAsk a Question
731 491 924 answers to any question