简体   繁体   English

如何显示不同表中的类别名称而不是类别ID

[英]How to display category name from different table instead of category id

I have 2 tables - one with forum category name field called "disp_name" and "ID" called "forum_cat" and onother with forum posts id, forum post content and cat_id and more called "forum" 我有2张表格-其中一张的论坛类别名称字段称为“ disp_name”,而另一张“ ID”称为“ forum_cat”另一张 具有论坛帖子ID,论坛帖子内容和cat_id, 更名 为“论坛”

I have model "Forum_cats" 我有模型“ Forum_cats”

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Forum_cats extends Model
    {

    protected $table = 'forum_cat';

    public $timestamps = false;

    }

and model "Forum" 和模型“论坛”

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Forum extends Model
{   

protected $table = 'forum';

public $timestamps = false;

}

Controller : 控制器:

public function index(){


    $forum = Forum::orderBy('timestamp', 'desc')->paginate(20);

//next variable is for different place
    $news = Neww::orderBy('date', 'DESC')->paginate(20);


    return view ('lapas.pamata.index',[
        'news'=>$news,
        'forum'=> $forum,
    ]);
}

Blade: 刀:

@foreach($forum as $forums)
          <li>
            <div class="media">
              <div class="media-body"> <a href="#" class="catg_title"> 
              {{$forums->title}}</a> </div>
              <i class="far fa-comment-alt"></i>&nbsp;{{$forums->comments}} 
              Kategorija:{{$forums->cat_id}}
            </div>
          </li>
        @endforeach

so the view at the moment is like this where after "Kategorija" i have only category id 所以目前的视图是这样的,在“ Kategorija”之后,我只有类别ID

How to make after name "Kategorija" output field "disp_name" from table "forum_cat". 如何在表“ forum_cat”中以名称“ Kategorija”为输出字段“ disp_name”。

Someone can tell that there are lots of posts about my problem but i am trying to solve this problem all day. 有人可以说关于我的问题有很多帖子,但是我整天都在努力解决这个问题。 I know that its about hasOne, belongsTo and hasMany but i dont understand how correct us them on my code. 我知道它关于hasOne,belongsTo和hasMany,但是我不明白我们在我的代码中如何对它们进行纠正。

So, I assume (and still not sure) that forum belongs to a category. 因此,我假设(但仍不确定)该论坛属于某个类别。

Your Forum model should be: 您的论坛模型应为:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Forum extends Model {   

    protected $table = 'forum';
    public $timestamps = false;

    public category(){
            return $this->belongsTo('App\Forum_cats', 'cat_id', 'id');
    }
}

And your blade should be: 您的刀片应该是:

@foreach($forum as $forums)
          <li>
            <div class="media">
              <div class="media-body"> <a href="#" class="catg_title"> 
              {{$forums->title}}</a> </div>
              <i class="far fa-comment-alt"></i>&nbsp;{{$forums->comments}} 
              Kategorija:{{$forums->category->disp_name}}
            </div>
          </li>
@endforeach

does that work for you? 那对你有用吗? You also may consider eager loads: 您还可以考虑渴望负载:

https://laravel.com/docs/5.8/eloquent-relationships#constraining-eager-loads https://laravel.com/docs/5.8/eloquent-relationships#constraining-eager-loads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM