Support the ongoing development of Laravel.io →
Database Eloquent

Terms table

  • term_id
  • name
  • slug

Term_taxonomy table

  • term_taxonomy_id
  • term_id
  • description

i want to show all record like "term_id , name , description"

my Term model

public function TermTaxonomy(){
    return $this->hasOne('TermTaxonomy');
}

my TermTaxonomy model

public function Term(){
    return $this->belongsTo('Term');
}

my route

$term = new Term;
$categories = $term->all(['term_id', 'name']);
foreach($categories as $category){
    echo $category->term_id . " " . $category->name . " " . "description of term_id should here" ; 
}

trying this code but Error Undefined property: Illuminate\Database\Eloquent\Collection::$TermTaxonomy

$e = new Term;
$f = $e->all(['term_id','name']);
    echo $f->TermTaxonomy->description;
}

with the above code i want to return all description of each term_id, but i am confusing why i can show description if the object just 1 , like this code below

$a = new Term;
$b = $a->all(['term_id','name'])->find(8);
echo $b->TermTaxonomy->description . "<br>"; // work return description of $b;

so what is exactly one to one relationship function ? is one to one only work when the object just 1 ?

what about in my case ? is my logic wrong to show all description using relationship method ? then what must i do to show term id , name , description lists ?

thanks in advance, i am totally newbie in laravel.

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

darkcyber darkcyber Joined 17 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.