dongyang4615 2015-01-15 09:12
浏览 57
已采纳

如何在laravel中显示来自不同表格的数据?

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.

  • 写回答

3条回答 默认 最新

  • dpbe81245 2015-01-15 12:52
    关注

    You can't access relationships on collections directly. I mean, what would you even expect? All descriptions as comma separated list?

    Instead you just access the relation when you loop over all of them anyways. (That's probably in the view so I'm going to use Blade syntax in this example)

    @foreach($terms as $term)
        {{ $term->term_id }} {{ $term->name }} {{ ($term->TermTaxonomy ? $term->TermTaxonomy->description : '') }}
    @endforeach
    

    ($term->TermTaxonomy ? $term->TermTaxonomy->description : '') is a shorthand if. It means:

    if($term->TermTaxonomy){
        echo $term->TermTaxonomy->description;
    }
    else {
        echo '';
    }
    

    It works because if the Term has no TermTaxonomy assigned, $term->TermTaxonomy will be null. With this code we only try to access ->description if TermTaxonomy exists and can therefore avoid a Trying to get property of non-object exception.


    Attention: While this works you definitely should eager load the TermTaxonomy relationship. You see if we leave it like that, each time you do $term->TermTaxonomy a DB query will be run. To avoid n+1 queries, use with to eager load relationships:

    $terms = Term::with('TermTaxonomy')->get(['term_id','name']);
    

    Regarding your question in the comments, yes you can limit the fields that you select from term_taxonomy:

    $terms = Term::with(['TermTaxonomy' => function($q){
        $q->select('term_id', 'description');
    }])->get(['term_id','name']);
    

    Just make sure you include the columns necessary to make the relationship (in this case term_id)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献