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 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题