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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化