duandi8613
2019-04-01 13:42
浏览 1.4k
已采纳

如何通过Laravel中的id获取用户名?

I am trying to create an offers forum, where some user can create their offers in order to provide their services and I want to show the name of the person that created that offer instead of the id.

In my database I have the two tables:

  • Offers table: Offers table

  • User table:

Users table

In offers I have a column of the professor_id, that is related to the id of users table.

This is what i have in my controller to show the offers:

public function ofertes(){

    $ofertes = Oferta::all()->sortByDesc('id');

    return view('create.ofertes')->with(compact('ofertes'));
}

and in the blade.php I have that code:

@foreach($ofertes as $oferta)
    <tr>
        <td>Nom : {{$oferta->professor_id}}</td> <br>
        <td>Títol : {{$oferta->titol}}</td> <br>
        <td>Descripció: {{$oferta->descripcio}}</td> <br>
        <td>Data: {{$oferta->created_at}}</td> <br><br>
    </tr>
@endforeach

and that is what is shown: ofertes.blade.php

Where it says nom, how I can show the name instead of the id?

Thank you!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试创建一个优惠论坛,其中一些用户可以创建他们的优惠以便提供他们的服务而我 想要显示创建该提议的人的名称而不是id。

在我的数据库中,我有两个表:

  • 优惠表:

  • 用户表:

    在优惠中,我有一个professor_id列,与用户表的id相关。

    这是我在我的控制器中显示的内容:

      public function ofertes(){
     
     $ ofertes = Oferta ::  all() - &gt; sortByDesc('id'); 
     
    返回视图('create.ofertes') - &gt; with(compact('ofertes')); 
    } 
       
     
     

    在blade.php中我有那段代码:

      @foreach($ ofertes as $ oferta)
    &lt; tr&gt; \  n   
     
     

    这就是显示的内容:

    如果说nom,我怎么能显示名称而不是 id?

    谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanliushua5026 2019-04-01 13:48
    已采纳

    If you have specified the relationship to professor in your Oferta model you can use the following code:

    public function ofertes(){
    
        $ofertes = Oferta::with('professor')->latest()->get();
    
        return view('create.ofertes')->with(compact('ofertes'));
    }
    

    Your blade:

    @foreach($ofertes as $oferta)
        <tr>
            <td>Nom : {{$oferta->professor->nom}}</td> <br>
            <td>Títol : {{$oferta->titol}}</td> <br>
            <td>Descripció: {{$oferta->descripcio}}</td> <br>
            <td>Data: {{$oferta->created_at}}</td> <br><br>
        </tr>
    @endforeach
    

    If you haven't specified the relation you should add the following method to your Oferta model (you might need to tweak this a little bit based on your namespaces):

    public function professor()
    {
        return $this->belongsTo(User::class);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanaoyuan7202 2019-04-01 13:51

    Join two table like

    $oferta = DB::table('users')
            ->join('offers','users.id','=','offers.professor_id')
            ->select('offers.*','users.nom')
            ->orderby('offers.id','DESC')->get();
    

    Then in place of {{$oferta->professor_id}}, replace {{$oferta->nom}}

    Your problem should be solved

    评论
    解决 无用
    打赏 举报
  • dqbr37828 2019-04-01 14:13

    Very easy

    public function ofertes(){
    
        $ofertes = Oferta::with('professor')->latest()->get();
        $users = User::all();
    
        return view('create.ofertes')->with(compact('ofertes', 'users'));
    }
    
    In blade file:
    
    <td>Nom : @foreach ($users as $user)
               @if ($oferta->professor_id === $user->id) $user->nom $user->cognom
               @endif
              @endforeach
    </td><br>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题