简体   繁体   English

如何从Laravel中的user_id中检索用户名?

[英]How retrive user name from user_id in laravel?

hellow i want to show 10 persons who has deposited amount in last24 hours . 我想告诉过去10小时内有10个人存入钱。 i am able to show there user_id and amount but i want users name instead of user_id. 我能够在其中显示user_id和数量,但我希望用户名而不是user_id。

i have no name colum in $funds it is in users i have this code: 我在$ funds中没有名称列,它在用户中,我有以下代码:

<div class="col-md-6">
    <div class="text-center"><h4 class="text-success">Last 10 Investors</h4></div>
    <table class="table text-white" >
        <tbody>
            @foreach( \App\Fund::where('created_at', '>', \Carbon\Carbon::now()->subHours(24))->orderBy('id', 'DESC')->take(10)->get() as $fund)
            <tr>
                <th scope="row"></th>
                <td>{{ $fund->user_id }}</td>
                <td class="text-center text-warning">{{ $fund->total }}</td>
                <td class="text-right"><img src="https://adsok.com/img/btc.png" alt="Bitcoin"> <i><b>bitcoin</b></i><br></td><br>
            </tr>@endforeach

        </tbody>
    </table> 
</div>

Set up a relation from your Fund Model to the User Model 建立从您的资金模型到用户模型的关系

In your Fund model, 在您的基金模型中,

public function user()
{
    return $this->belongsTo('App\User', 'user_id'); 
}

and you could access the User that belongs to the fund as 您可以通过以下方式访问属于该基金的用户

$fund->user->{your-username-field}

OR if you don't want to set up a relationship and fetch the username, you could do so by 或者,如果您不想建立关系并获取用户名,则可以通过以下方式实现

$user = \App\User::find($fund->user_id);
if (! empty($user)) {
    $user_name = $user->user_name; // or whatever the username field is
}

Try like this : 尝试这样:

@foreach( \App\Fund::select("*")->with('users')->where('created_at', '>', \Carbon\Carbon::now()->subHours(24))->orderBy('id', 'DESC')->take(10)->get()->toArray() as $fund)
    <tr>
      <th scope="row"></th>
      <td>{{ $fund['user']['user_name'] }}</td>
       <td class="text-center text-warning">{{ $fund->total }}</td>
      <td class="text-right"><img src="https://adsok.com/img/btc.png" alt="Bitcoin"> <i><b>bitcoin</b></i><br></td><br>
    </tr>@endforeach

so you have to models: "Fund" and "User". 因此您必须建立模型:“资金”和“用户”。
you should connect them with Laravel's Relationships: 您应该将它们与Laravel的关系联系起来:
add this method to "User" model: 将此方法添加到“用户”模型中:

public function funds()
{
    return $this->hasMany('App\Fund', 'user_id');
}

then in "Fund" model: 然后在“资金”模型中:

public function user()
{
    return $this->belongsTo('App\User', 'user_id');
}   

now you can access user's name from funds: 现在您可以从资金中访问用户名:
i suggest you to retrive your datas in controller, not views: 我建议您在控制器而不是视图中检索数据:

public function index()
{
    $funds = Fund::with('user')->
        where('created_at', '>', \Carbon\Carbon::now()->
            subHours(24))->orderBy('id', 'DESC')->take(10)->get();
    return view('viewName', ['funds' => $funds]);
}  

then in view: 然后来看:

@foreach ($funds as $fund)
    {{ $fund->user->name }}
@endforeach

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

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