G
G
Givi7772019-09-19 17:27:40
Laravel
Givi777, 2019-09-19 17:27:40

How to get the name from a model link?

table company
colum "category"

table category
colum "id", "name"

Company hasMany
public function getCategory(){
        return $this->hasMany(Category::class, 'id');
}

trying to get the category name?
{{$com->category->name}}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2019-09-19
@Givi777

If your relationship is one to one (each company can have one category)
then so

return $this->hasOne(Category::class, 'id', 'category');

and you have a connection called - getCategory,
respectively, the call should be
{{$com->getCategory->name}}

G
Givi777, 2019-09-19
@Givi777

Anatoly
changed places
because there is no such category field in category table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question